求救,奇怪問題! 導出的Excel文件,打開時沒有內容,可是預覽或者全蘋顯示時有內容? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 //取得導出數據 DataSet ds=dsSource; if(ds == null) { return; } //彈出導出對話框文件設置 string saveFileName = ""; bool fileSaved=false; SaveFileDialog saveDialog = new SaveFileDialog(); saveDialog.DefaultExt = "xls"; saveDialog.Filter="Excel文件|*.xls"; saveDialog.ShowDialog(); saveFileName = saveDialog.FileName; if(saveFileName.IndexOf(":")<0) //取消返回 { return; } Excel.Application xlApp = new Excel.Application(); if(xlApp==null) { MessageBox.Show("無法創見Excel對象,請確定已安裝Excel軟是軟件!"); return; } Excel.Workbooks workbooks=xlApp.Workbooks; Excel.Workbook workbook=workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet); Excel.Worksheet worksheet=(Excel.Worksheet)workbook.Worksheets[1];//取得sheet1 Excel.Range range; string oldCaption=this.Text; long totalCount=ds.Tables[0].Rows.Count; long rowRead=0; float percent=0; worksheet.Cells[1,1]=this.Text; //寫入子段 for(int i=0;i<ds.Tables[0].Columns.Count;i++) { worksheet.Cells[2,i+1]=ds.Tables[0].Columns[i].ColumnName; range=(Excel.Range)worksheet.Cells[2,i+1]; range.Interior.ColorIndex = 15; range.Font.Bold = true; } //寫入數值 for(int r=0;r<ds.Tables[0].Rows.Count;r++) { for(int i=0;i<ds.Tables[0].Columns.Count;i++) { worksheet.Cells[r+3,i+1]=ds.Tables[0].Rows[r][i]; } rowRead++; percent=((float)(100*rowRead))/totalCount; this.Text = "正在導出數據:["+ percent.ToString("0.00") +"%]..."; Application.DoEvents(); } this.Text = oldCaption; range=worksheet.get_Range(worksheet.Cells[2,1],worksheet.Cells[ds.Tables[0].Rows.Count+2,ds.Tables[0].Columns.Count]); range.BorderAround(Excel.XlLineStyle.xlContinuous,Excel.XlBorderWeight.xlThin,Excel.XlColorIndex.xlColorIndexAutomatic,null); range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic; range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].LineStyle =Excel.XlLineStyle.xlContinuous; range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].Weight =Excel.XlBorderWeight.xlThin; if(ds.Tables[0].Columns.Count>1) { range.Borders[Excel.XlBordersIndex.xlInsideVertical].ColorIndex =Excel.XlColorIndex.xlColorIndexAutomatic; range.Borders[Excel.XlBordersIndex.xlInsideVertical].LineStyle = Excel.XlLineStyle.xlContinuous; range.Borders[Excel.XlBordersIndex.xlInsideVertical].Weight = Excel.XlBorderWeight.xlThin; } if(saveFileName!="") { try { workbook.Saved =true; workbook.SaveCopyAs(saveFileName); fileSaved=true; MessageBox.Show("導出文件成功!"); } catch(Exception ex) { fileSaved=false; MessageBox.Show("導出文件時出錯,文件可能正被打開!\n"+ex.Message); } } else { fileSaved=false; } xlApp.Workbooks.Close(); xlApp.Quit(); GC.Collect();//強行銷毀 if(fileSaved && File.Exists(saveFileName)) { System.Diagnostics.Process.Start(saveFileName); } 我是将它输出到页面再导出的StringWriter stringWriter = new StringWriter(); HtmlTextWriter htmlWriter = new HtmlTextWriter( stringWriter ); dgCountList.RenderControl(htmlWriter); string filePath = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Day.ToString() + "-" + System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString() + System.DateTime.Now.Millisecond.ToString() + ".xls";//文件名 string path = @"Log"; string dir = Server.MapPath(path); string filestr = dir+@"\"+filePath; //filePath是文件的路径 if(!System.IO.Directory.Exists(dir)) { System.IO.Directory.CreateDirectory(dir); } System.IO.StreamWriter sw = new StreamWriter(filestr); sw.Write(stringWriter.ToString()); sw.Close(); this.RegisterStartupScript("suc","<script>window.open('log/"+filePath+"');</script>");dgCountList就是存放数据的那个datagrid. 怎么用C# 把核心代码编译成二进制数据 winform,如何在Gridview单元格中光标处插入字符? rdlc报表 XML 动态的产生 richtextbox设置输入光标的位置 TextBox的内容怎么能按照字段读取?(C# Win Frm) 强烈推荐 虚拟地球 源代码下载 调用API问题 动态添加类的属性问题(求助高手) --== 关于XSL中的脚本的参数问题 ==-- 求助 谁用过 Aspose.Cells,好像XP上不可以用。 谁能给我个时间控件用啊 如何检测显卡的接口类型是AGP还是PCI,或者是PCIE,在.NET中??????????高分相送!
DataSet ds=dsSource;
if(ds == null)
{
return;
} //彈出導出對話框文件設置
string saveFileName = "";
bool fileSaved=false;
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.DefaultExt = "xls";
saveDialog.Filter="Excel文件|*.xls";
saveDialog.ShowDialog();
saveFileName = saveDialog.FileName;
if(saveFileName.IndexOf(":")<0) //取消返回
{
return;
} Excel.Application xlApp = new Excel.Application(); if(xlApp==null)
{
MessageBox.Show("無法創見Excel對象,請確定已安裝Excel軟是軟件!");
return;
} Excel.Workbooks workbooks=xlApp.Workbooks;
Excel.Workbook workbook=workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
Excel.Worksheet worksheet=(Excel.Worksheet)workbook.Worksheets[1];//取得sheet1
Excel.Range range; string oldCaption=this.Text;
long totalCount=ds.Tables[0].Rows.Count;
long rowRead=0;
float percent=0; worksheet.Cells[1,1]=this.Text;
//寫入子段
for(int i=0;i<ds.Tables[0].Columns.Count;i++)
{
worksheet.Cells[2,i+1]=ds.Tables[0].Columns[i].ColumnName;
range=(Excel.Range)worksheet.Cells[2,i+1];
range.Interior.ColorIndex = 15;
range.Font.Bold = true; }
//寫入數值
for(int r=0;r<ds.Tables[0].Rows.Count;r++)
{
for(int i=0;i<ds.Tables[0].Columns.Count;i++)
{
worksheet.Cells[r+3,i+1]=ds.Tables[0].Rows[r][i];
}
rowRead++;
percent=((float)(100*rowRead))/totalCount;
this.Text = "正在導出數據:["+ percent.ToString("0.00") +"%]...";
Application.DoEvents();
}
this.Text = oldCaption; range=worksheet.get_Range(worksheet.Cells[2,1],worksheet.Cells[ds.Tables[0].Rows.Count+2,ds.Tables[0].Columns.Count]);
range.BorderAround(Excel.XlLineStyle.xlContinuous,Excel.XlBorderWeight.xlThin,Excel.XlColorIndex.xlColorIndexAutomatic,null); range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;
range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].LineStyle =Excel.XlLineStyle.xlContinuous;
range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].Weight =Excel.XlBorderWeight.xlThin; if(ds.Tables[0].Columns.Count>1)
{
range.Borders[Excel.XlBordersIndex.xlInsideVertical].ColorIndex =Excel.XlColorIndex.xlColorIndexAutomatic;
range.Borders[Excel.XlBordersIndex.xlInsideVertical].LineStyle = Excel.XlLineStyle.xlContinuous;
range.Borders[Excel.XlBordersIndex.xlInsideVertical].Weight = Excel.XlBorderWeight.xlThin;
} if(saveFileName!="")
{
try
{
workbook.Saved =true;
workbook.SaveCopyAs(saveFileName);
fileSaved=true;
MessageBox.Show("導出文件成功!");
}
catch(Exception ex)
{
fileSaved=false;
MessageBox.Show("導出文件時出錯,文件可能正被打開!\n"+ex.Message);
}
}
else
{
fileSaved=false;
}
xlApp.Workbooks.Close();
xlApp.Quit();
GC.Collect();//強行銷毀 if(fileSaved && File.Exists(saveFileName))
{
System.Diagnostics.Process.Start(saveFileName);
}
HtmlTextWriter htmlWriter = new HtmlTextWriter( stringWriter ); dgCountList.RenderControl(htmlWriter);
string filePath = System.DateTime.Now.Year.ToString()
+ System.DateTime.Now.Month.ToString()
+ System.DateTime.Now.Day.ToString() + "-"
+ System.DateTime.Now.Hour.ToString()
+ System.DateTime.Now.Minute.ToString()
+ System.DateTime.Now.Second.ToString()
+ System.DateTime.Now.Millisecond.ToString() + ".xls";//文件名
string path = @"Log";
string dir = Server.MapPath(path);
string filestr = dir+@"\"+filePath; //filePath是文件的路径 if(!System.IO.Directory.Exists(dir))
{
System.IO.Directory.CreateDirectory(dir);
} System.IO.StreamWriter sw = new StreamWriter(filestr);
sw.Write(stringWriter.ToString());
sw.Close(); this.RegisterStartupScript("suc","<script>window.open('log/"+filePath+"');</script>");dgCountList就是存放数据的那个datagrid.