文档介绍:第 1 页
在日常工作中,大家都习惯Office作为办公软件,因此,在开发软件时,常常会有把数据导出到Excel等Office软件需求。在此,收集一些常用导出文件源程序,希望给大家带来方便。〔不断更新〕
一、DataSet数据集内数据转/// 将指定Dataset导出到Excel文件
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public bool ExportToExcel( ds, string ReportName)
第 5 页
if ([0]. == 0)
("数据集为空");
xlapp = new ApplicationClass();
Workbook xlbook = (true);
Worksheet xlsheet = (Worksheet)[1];
Range range = ([1, 1], [1, [0].]);
= true;
= ReportName;
= 20;
= true;
= ;
int colIndex = 0;
int RowIndex = 2;
//开场写入每列标题
foreach (DataColumn dc in [0].Columns)
colIndex++;
[RowIndex, colIndex] = ;
//开场写入内容
第 6 页
int RowCount = [0].;//行数
for (int i = 0; i < RowCount; i++)
RowIndex++;
int ColCount = [0].;//列数
for (colIndex = 1; colIndex <= ColCount; colIndex++)
[RowIndex, colIndex] = [0].Rows[i][colIndex - 1];//dg[i, colIndex - 1];
= [0].Rows[i][colIndex - 1].ToString().Length;
= true;
();
();
();
return true;
public bool ExportToExcelOF( ds, string ReportName)
if ([0]. == 0)
("数据集为空");
string = ;
// dt = new ();
第 7 页
obj;
StreamWriter objStreamWriter;
string strLine = "";
obj = new (, , );
objStreamWriter = new StreamWriter(obj, );
strLine = ReportName;
(strLine);
strLine = "";
for (int i