C#中数据导出到Excel表格Word下载.docx
- 文档编号:21772065
- 上传时间:2023-02-01
- 格式:DOCX
- 页数:22
- 大小:24.83KB
C#中数据导出到Excel表格Word下载.docx
《C#中数据导出到Excel表格Word下载.docx》由会员分享,可在线阅读,更多相关《C#中数据导出到Excel表格Word下载.docx(22页珍藏版)》请在冰豆网上搜索。
17.
18.//变量定义
19.stringcolHeaders=null;
20.stringIs_item=null;
21.
22.//显示格式定义////////////////
23.
24.
25.//文件流操作定义
26.//FileStreamfs=newFileStream(FileName,FileMode.Create,FileAccess.Write);
27.//StreamWritersw=newStreamWriter(fs,System.Text.Encoding.GetEncoding("
));
28.
29.StringWritersfw=newStringWriter();
30.//定义表对象与行对象,同时用DataSet对其值进行初始化
31.System.Data.DataTabledt=ds.Tables[0];
32.DataRow[]myRow=dt.Select();
33.inti=0;
34.intcl=dt.Columns.Count;
35.
36.//取得数据表各列标题,各标题之间以/t分割,最后一个列标题后加回车符
37.for(i=0;
i<
cl;
i++)
38.{
39.//if(i==(cl-1))//最后一列,加/n
40.//colHeaders+=dt.Columns[i].Caption.ToString();
41.//else
42.colHeaders+=dt.Columns[i].Caption.ToString()+"
/t"
43.}
44.sfw.WriteLine(colHeaders);
45.//sw.WriteLine(colHeaders);
46.
47.//逐行处理数据
48.foreach(DataRowrowinmyRow)
49.{
50.//当前数据写入
51.for(i=0;
52.{
53.//if(i==(cl-1))
54.//Is_item+=row[i].ToString()+"
/n"
55.//else
56.Is_item+=row[i].ToString()+"
57.}
58.sfw.WriteLine(Is_item);
59.//sw.WriteLine(Is_item);
60.Is_item=null;
61.}
62.resp.Write(sfw);
63.//resp.Clear();
64.resp.End();
65.}
66.catch(Exceptione)
67.{
68.throwe;
69.}
70.}
二、DataSet数据集内数据转化为Excel文件
(2)
1.///<
summary>
2.///ExportFiles的摘要说明。
3.///作用:
把DataSet数据集内数据转化为Excel文件
4.///描述:
导出Excel文件
5.///备注:
6.///<
/summary>
7.publicclassExportFiles
9.privatestringfilePath="
"
10.publicExportFiles(stringexcel_path)
11.{
12.//
13.//TODO:
在此处添加构造函数逻辑
14.//
15.filePath=excel_path;
16.}
17.///<
18.///将指定的Dataset导出到Excel文件
19.///<
20.///<
paramname="
dt"
>
<
/param>
21.///<
returns>
/returns>
22.publicboolExportToExcel(System.Data.DataSetds,stringReportName)
23.{
24.if(ds.Tables[0].Rows.Count==0)
25.{
26.MessageBox.Show("
数据集为空"
27.}
28.Microsoft.Office.Interop.Excel._Applicationxlapp=newApplicationClass();
29.Workbookxlbook=xlapp.Workbooks.Add(true);
30.Worksheetxlsheet=(Worksheet)xlbook.Worksheets[1];
31.Rangerange=xlsheet.get_Range(xlapp.Cells[1,1],xlapp.Cells[1,ds.Tables[0].Columns.Count]);
32.range.MergeCells=true;
33.xlapp.ActiveCell.FormulaR1C1=ReportName;
34.xlapp.ActiveCell.Font.Size=20;
35.xlapp.ActiveCell.Font.Bold=true;
36.xlapp.ActiveCell.HorizontalAlignment=Microsoft.Office.Interop.Excel.Constants.xlCenter;
37.intcolIndex=0;
38.intRowIndex=2;
39.//开始写入每列的标题
40.foreach(DataColumndcinds.Tables[0].Columns)
41.{
42.colIndex++;
43.xlsheet.Cells[RowIndex,colIndex]=dc.Caption;
44.}
45.//开始写入内容
46.intRowCount=ds.Tables[0].Rows.Count;
//行数
47.for(inti=0;
i<
RowCount;
i++)
48.{
49.RowIndex++;
50.intColCount=ds.Tables[0].Columns.Count;
//列数
51.for(colIndex=1;
colIndex<
=ColCount;
colIndex++)
53.xlsheet.Cells[RowIndex,colIndex]=ds.Tables[0].Rows[i][colIndex-1];
//dg[i,colIndex-1];
54.xlsheet.Cells.ColumnWidth=ds.Tables[0].Rows[i][colIndex-1].ToString().Length;
55.}
56.}
57.
58.xlbook.Saved=true;
59.xlbook.SaveCopyAs(filePath);
60.xlapp.Quit();
61.GC.Collect();
62.returntrue;
63.}
64.
65.publicboolExportToExcelOF(System.Data.DataSetds,stringReportName)
66.{
67.if(ds.Tables[0].Rows.Count==0)
68.{
69.MessageBox.Show("
70.}
71.stringFileName=filePath;
72.
73.//System.Data.DataTabledt=newSystem.Data.DataTable();
74.FileStreamobjFileStream;
75.StreamWriterobjStreamWriter;
76.stringstrLine="
77.objFileStream=newFileStream(FileName,FileMode.OpenOrCreate,FileAccess.Write);
78.objStreamWriter=newStreamWriter(objFileStream,System.Text.Encoding.Unicode);
79.
80.strLine=ReportName;
81.objStreamWriter.WriteLine(strLine);
82.strLine="
83.
84.for(inti=0;
ds.Tables[0].Columns.Count;
85.{
86.strLine=strLine+ds.Tables[0].Columns[i].ColumnName.ToString()+"
"
+Convert.ToChar(9);
87.}
88.objStreamWriter.WriteLine(strLine);
89.strLine="
90.
91.for(inti=0;
ds.Tables[0].Rows.Count;
92.{
93.strLine=strLine+(i+1)+Convert.ToChar(9);
94.for(intj=1;
j<
j++)
95.{
96.strLine=strLine+ds.Tables[0].Rows[i][j].ToString()+Convert.ToChar(9);
97.}
98.objStreamWriter.WriteLine(strLine);
99.strLine="
100.}
101.objStreamWriter.Close();
102.objFileStream.Close();
103.
104.//Microsoft.Office.Interop.Excel._Applicationxlapp=newApplicationClass();
105.//Workbookxlbook=xlapp.Workbooks.Add(true);
106.//Worksheetxlsheet=(Worksheet)xlbook.Worksheets[1];
107.//Rangerange=xlsheet.get_Range(xlapp.Cells[1,1],xlapp.Cells[1,ds.Tables[0].Columns.Count]);
108.//range.EntireColumn.AutoFit();
109.//xlapp.Quit();
110.returntrue;
111.}
112.}
三、生成XML然后转换成Excel方式
参考资源:
(源程序)
优点:
a.服务端不用安装Excel程序。
b.支持一定的Excel文件格式设置,比如字体大小、颜色、合并单元格等。
缺点:
a.与Excel2000不兼容:
由于Excel2000不支持XML,所以以这种方法生成的Excel文件可能在Excel2000中不兼容(毕竟目前还有不少用户的电脑装的是Excel2000)。
b.可能不支持Excel文件页边距的设置;
不支持Excel文件横向、纵向的设置;
不支持Excel模板;
c.编程工作量比较大;
d.生成的文件本质上是XML文件,需要“另存为xls”才能变成真正的Excel文件。
e.性能是好是坏还不清楚,目前还没真正在项目中用过。
希望有用过此方案的朋友能介绍一下这个方案的性能。
四、导出GridView到Excel
1.//导出GridView到Excel中的关键之处
2.//用法:
ToExcel(GVStaff,TextBox1.Text);
3.
4.publicstaticvoidToExcel(System.Web.UI.Controlctl,stringFileName)
5.{
6.HttpContext.Current.Response.Charset="
UTF-8"
7.HttpContext.Current.Response.ContentEncoding=System.Text.Encoding.Default;
8.HttpContext.Current.Response.ContentType="
9.HttpContext.Current.Response.AppendHeader("
Content-Disposition"
+"
10.ctl.Page.EnableViewState=false;
11.System.IO.StringWritertw=newSystem.IO.StringWriter();
12.HtmlTextWriterhw=newHtmlTextWriter(tw);
13.ctl.RenderControl(hw);
14.HttpContext.Current.Response.Write(tw.ToString());
15.HttpContext.Current.Response.End();
18.必须有下面这句!
否则不会通过!
19.publicoverridevoidVerifyRenderingInServerForm(Controlcontrol)
20.{
21.//ConfirmsthatanHtmlFormcontrolisrenderedfor
22.}
五、DataTable导出到Excel
1.usingSystem;
2.usingMicrosoft.Office.Interop.Excel;
3.usingSystem.Windows.Forms;
4.namespaceDongVI
7.///DataTable导出到Excel
8.///整理:
dongVi
9.///<
10.publicclassDataTableToExcel
12.privateDataTableToExcel()
13.{
14.
15.}
16.///<
17.///导出Excel
18.///<
要导出的DataTable<
20.publicstaticvoidExportToExcel(System.Data.DataTabledt)
21.{
22.if(dt==null)return;
24.Microsoft.Office.Interop.Excel.ApplicationxlApp=newMicrosoft.Office.Interop.Excel.Application();
25.if(xlApp==null)
26.{
27.//lblMsg.Text="
无法创建Excel对象,可能您的电脑未安装Excel"
28.MessageBox.Show("
);
29.return;
30.}
31.System.Windows.Forms.SaveFileDialogsaveDia=newSaveFileDialog();
32.saveDia.Filter="
Excel|*.xls"
33.saveDia.Title="
导出为Excel文件"
34.if(saveDia.ShowDialog()==System.Windows.Forms.DialogResult.OK
35.&
&
!
string.Empty.Equals(saveDia.FileName))
36.{
37.Microsoft.Office.Interop.Excel.Workbooksworkbooks=xlApp.Workbooks;
38.Microsoft.Office.Interop.Excel.Workbookworkbook=workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
39.Microsoft.Office.Interop.Excel.Worksheetworksheet=(Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];
//取得sheet1
40.Microsoft.Office.Interop.Excel.Rangerange=null;
41.longtotalCount=dt.Rows.Count;
42.longrowRead=0;
43.floatpercent=0;
44.stringfileName=saveDia.FileName;
45.//写入标题
46.for(inti=0;
dt.Columns.Count;
47.{
48.worksheet.Cells[1,i+1]=dt.Columns[i].ColumnName;
49.range=(Microsoft.Office.Interop.Excel.Range)worksheet.Cells[1,i+1];
50.//range.Interior.ColorIndex=15;
//背景颜色
51.range.Font.Bold=true;
//粗体
52.range.HorizontalAlignment=Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
//居中
53.//加边框
54.range.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous,Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin,Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic,null);
55.//range.ColumnWidth=4.63;
//设置列宽
56.//range.EntireColumn.AutoFit();
//自动调整列宽
57.//r1.EntireRow.AutoFit();
//自动调整行高
58.}
59.//写入内容
60.for(intr=0;
r<
dt.DefaultView.Count;
r++)
61.{
62.for(inti=0;
63.{
64.worksheet.Cells[r+2,i+1]=dt.DefaultView[r][i];
65.range=(Microsoft.Office.Interop.Excel.Range)worksheet.Cells[r+2,i+1];
66.range.Font.Size=9;
//字体大小
67.//加边框
68.range.BorderAround(Mic
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C# 数据 导出 Excel 表格
![提示](https://static.bdocx.com/images/bang_tan.gif)