.NET开发人员首选的方法,通过COM组件调用Office软件本身来实现文件的创建和读写,但是数据量较大的时候异常缓慢;如下代码所示已经做了优化,将一个二维对象数组赋值到一个单元格区域中(下面的代码中只能用于导出列数不多于26列的数据导出):
OfficePIA
代码如下:
publicstaticvoidExportToExcel(DataSetdataSet,stringoutputPath)
{
Excel.ApplicationClassexcel=newExcel.ApplicationClass();
Excel.Workbookworkbook=excel.Workbooks.Add(Type.Missing);
intsheetIndex=0;
foreach(System.Data.DataTabledtindataSet.Tables)
{
object[,]data=newobject[dt.Rows.Count+1,dt.Columns.Count];
for(intj=0;j<dt.Columns.Count;j++)
{
data[0,j]=dt.Columns[j].ColumnName;
}
for(intj=0;j<dt.Columns.Count;j++)
{
for(inti=0;i<dt.Rows.Count;i++)
{
data[i+1,j]=dt.Rows[i][j];
}
}
stringfinalColLetter=string.Empty;
stringcolCharset=”ABCDEFGHIJKLMNOPQRSTUVWXYZ”;
intcolCharsetLen=colCharset.Length;
if(dt.Columns.Count>colCharsetLen)
{
finalColLetter=colCharset.Substring(
(dt.Columns.Count-1)/colCharsetLen-1,1);
}
finalColLetter+=colCharset.Substring(
(dt.Columns.Count-1)%colCharsetLen,1);
Excel.Worksheetsheet=(Excel.Worksheet)workbook.Sheets.Add(
workbook.Sheets.get_Item(++sheetIndex),
Type.Missing,1,Excel.XlSheetType.xlWorksheet);
sheet.Name=dt.TableName;
stringrange=string.Format(“A1:{0}{1}”,finalColLetter,dt.Rows.Count+1);
sheet.get_Range(range,Type.Missing).Value2=data;
((Excel.Range)sheet.Rows[1,Type.Missing]).Font.Bold=true;
}
workbook.SaveAs(outputPath,Excel.XlFileFormat.xlWorkbookNormal,Type.Missing,
Type.Missing,Type.Missing,Type.Missing,Excel.XlSaveAsAccessMode.xlExclusive,
Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);
workbook.Close(true,Type.Missing,Type.Missing);
workbook=null;
excel.Quit();
KillSpecialExcel(excel);
excel=null;
GC.Collect();
GC.WaitForPendingFinalizers();
}
[DllImport(“user32.dll”,SetLastError=true)]
staticexternintGetWindowThreadProcessId(IntPtrhWnd,outintprocessId);
staticvoidKillSpecialExcel(Excel.Applicationapp)
{
try
{
if(app!=null)
{
intprocessId;
GetWindowThreadProcessId(newIntPtr(app.Hwnd),outprocessId);
System.Diagnostics.Process.GetProcessById(processId).Kill();
}
}
catch(Exceptionex)
{
throwex;
}
}
文件流
这种方法的效率明显高于第一种,而且也不需要安装Office,但是导出的xls文件并不符合Excel的格式标准,在打开生成的xls文件时会提示:Thefileyouaretryingtoopenisinadifferentformatthatspecifiedbythefileextension.Verifythatthefileisnotcorruptedandisfromatrustedsourcebeforeopeningthefile.
代码如下:
publicstaticvoidExportToExcel(System.Data.DataSetds,stringpath)
{
StreamWritersw=null;
try
{
longtotalCount=ds.Tables[0].Rows.Count;
sw=newStreamWriter(path,false,Encoding.Unicode);
StringBuildersb=newStringBuilder();
for(inti=0;i<ds.Tables[0].Columns.Count;i++)
{
sb.Append(ds.Tables[0].Columns[i].ColumnName+”t”);
}
sb.Append(Environment.NewLine);
for(inti=0;i<ds.Tables[0].Rows.Count;i++)
{
for(intj=0;j<ds.Tables[0].Columns.Count;j++)
{
sb.Append(ds.Tables[0].Rows[i][j].ToString()+”t”);
}
sb.Append(Environment.NewLine);
}
sw.Write(sb.ToString());
sw.Flush();
}
catch(IOExceptionioe)
{
throwioe;
}
finally
{
if(sw!=null)
{
sw.Close();
}
}
}
上述就是C#学习教程:C# WinForm导出Excel方法介绍分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)! 您可能感兴趣的文章:C#数据导入/导出Excel文件及winForm导出Execl
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/905056.html