导出Excel文件Word文档格式.docx

上传人:b****3 文档编号:7641884 上传时间:2023-05-08 格式:DOCX 页数:13 大小:19.93KB
下载 相关 举报
导出Excel文件Word文档格式.docx_第1页
第1页 / 共13页
导出Excel文件Word文档格式.docx_第2页
第2页 / 共13页
导出Excel文件Word文档格式.docx_第3页
第3页 / 共13页
导出Excel文件Word文档格式.docx_第4页
第4页 / 共13页
导出Excel文件Word文档格式.docx_第5页
第5页 / 共13页
导出Excel文件Word文档格式.docx_第6页
第6页 / 共13页
导出Excel文件Word文档格式.docx_第7页
第7页 / 共13页
导出Excel文件Word文档格式.docx_第8页
第8页 / 共13页
导出Excel文件Word文档格式.docx_第9页
第9页 / 共13页
导出Excel文件Word文档格式.docx_第10页
第10页 / 共13页
导出Excel文件Word文档格式.docx_第11页
第11页 / 共13页
导出Excel文件Word文档格式.docx_第12页
第12页 / 共13页
导出Excel文件Word文档格式.docx_第13页
第13页 / 共13页
亲,该文档总共13页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

导出Excel文件Word文档格式.docx

《导出Excel文件Word文档格式.docx》由会员分享,可在线阅读,更多相关《导出Excel文件Word文档格式.docx(13页珍藏版)》请在冰点文库上搜索。

导出Excel文件Word文档格式.docx

this.EnableViewState 

false;

这里我们利用了ContentType属性,它默认的属性为text/html,这时将输出为超文本,即我们常见的网页格式到客户端,如果改为ms-excel将将输出excel格式,也就是说以电子表格的格式输出到客户端,这时浏览器将提示你下载保存。

ContentType的属性还包括:

image/JPEG;

text/HTML;

image/GIF;

vnd.ms-excel/msword。

同理,我们也可以输出(导出)图片、word文档等。

下面的方法,也均用了这个属性。

2、将DataGrid控件中的数据导出excel

上述方法虽然实现了导出的功能,但同时把按钮、分页框等html中的所有输出信息导了进去。

而我们一般要导出的是数据,DataGrid控件上的数据。

System.Web.UI.Controlctl=this.DataGrid1;

//DataGrid1是你在窗体中拖放的控件

HttpContext.Current.Response.AppendHeader("

filename=Excel.xls"

HttpContext.Current.Response.Charset="

UTF-8"

HttpContext.Current.Response.ContentEncoding=System.Text.Encoding.Default;

HttpContext.Current.Response.ContentType="

application/ms-excel"

ctl.Page.EnableViewState=false;

System.IO.StringWriter 

tw=newSystem.IO.StringWriter();

System.Web.UI.HtmlTextWriterhw=newSystem.Web.UI.HtmlTextWriter(tw);

ctl.RenderControl(hw);

HttpContext.Current.Response.Write(tw.ToString());

HttpContext.Current.Response.End();

如果你的DataGrid用了分页,它导出的是当前页的信息,也就是它导出的是DataGrid中显示的信息。

而不是你select语句的全部信息。

为方便使用,写成方法如下:

publicvoidDGToExcel(System.Web.UI.Controlctl) 

{

HttpContext.Current.Response.AppendHeader("

HttpContext.Current.Response.Charset="

HttpContext.Current.Response.ContentEncoding=System.Text.Encoding.Default;

HttpContext.Current.Response.ContentType="

ctl.Page.EnableViewState=false;

System.IO.StringWriter 

System.Web.UI.HtmlTextWriterhw=newSystem.Web.UI.HtmlTextWriter(tw);

ctl.RenderControl(hw);

HttpContext.Current.Response.Write(tw.ToString());

HttpContext.Current.Response.End();

用法:

DGToExcel(datagrid1);

3、将DataSet中的数据导出excel

有了上边的思路,就是将在导出的信息,输出(Response)客户端,这样就可以导出了。

那么把DataSet中的数据导出,也就是把DataSet中的表中的各行信息,以ms-excel的格式Response到http流,这样就OK了。

说明:

参数ds应为填充有数据表的DataSet,文件名是全名,包括后缀名,如excel2006.xls

public 

voidCreateExcel(DataSetds,stringFileName) 

{

HttpResponseresp;

resp=Page.Response;

resp.ContentEncoding=System.Text.Encoding.GetEncoding("

GB2312"

resp.AppendHeader("

"

+FileName);

stringcolHeaders="

"

ls_item="

//定义表对象与行对象,同时用DataSet对其值进行初始化

DataTabledt=ds.Tables[0];

DataRow[]myRow=dt.Select();

//可以类似dt.Select("

id>

10"

)之形式达到数据筛选目的

inti=0;

intcl=dt.Columns.Count;

//取得数据表各列标题,各标题之间以t分割,最后一个列标题后加回车符

for(i=0;

{

if(i==(cl-1))//最后一列,加n

{

colHeaders+=dt.Columns[i].Caption.ToString()+"

n"

}

else

colHeaders+=dt.Columns[i].Caption.ToString()+"

t"

resp.Write(colHeaders);

//向HTTP输出流中写入取得的数据信息

//逐行处理数据 

foreach(DataRowrowinmyRow)

//当前行数据写入HTTP输出流,并且置空ls_item以便下行数据 

ls_item+=row[i].ToString()+"

ls_item+=row[i].ToString()+"

resp.Write(ls_item);

ls_item="

resp.End();

}

4、将dataview导出excel

若想实现更加富于变化或者行列不规则的excel导出时,可用本法。

publicvoidOutputExcel(DataViewdv,stringstr)

//dv为要输出到Excel的数据,str为标题名称

GC.Collect();

Applicationexcel;

//=newApplication();

introwIndex=4;

intcolIndex=1;

_WorkbookxBk;

_WorksheetxSt;

excel=newApplicationClass();

xBk=excel.Workbooks.Add(true);

xSt=(_Worksheet)xBk.ActiveSheet;

//

//取得标题

foreach(DataColumncolindv.Table.Columns)

colIndex++;

excel.Cells[4,colIndex]=col.ColumnName;

xSt.get_Range(excel.Cells[4,colIndex],excel.Cells[4,colIndex]).HorizontalAlignment=XlVAlign.xlVAlignCenter;

//设置标题格式为居中对齐

}

//取得表格中的数据

foreach(DataRowViewrowindv)

rowIndex++;

colIndex=1;

colIndex++;

if(col.DataType==System.Type.GetType("

System.DateTime"

))

excel.Cells[rowIndex,colIndex]=(Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("

yyyy-MM-dd"

xSt.get_Range(excel.Cells[rowIndex,colIndex],excel.Cells[rowIndex,colIndex]).HorizontalAlignment=XlVAlign.xlVAlignCenter;

//设置日期型的字段格式为居中对齐

else

System.String"

excel.Cells[rowIndex,colIndex]="

'

+row[col.ColumnName].ToString();

//设置字符型的字段格式为居中对齐

excel.Cells[rowIndex,colIndex]=row[col.ColumnName].ToString();

//加载一个合计行

introwSum=rowIndex+1;

intcolSum=2;

excel.Cells[rowSum,2]="

合计"

xSt.get_Range(excel.Cells[rowSum,2],excel.Cells[rowSum,2]).HorizontalAlignment=XlHAlign.xlHAlignCenter;

//设置选中的部分的颜色

xSt.get_Range(excel.Cells[rowSum,colSum],excel.Cells[rowSum,colIndex]).Select();

xSt.get_Range(excel.Cells[rowSum,colSum],excel.Cells[rowSum,colIndex]).Interior.ColorIndex=19;

//设置为浅黄色,共计有56种

//取得整个报表的标题

excel.Cells[2,2]=str;

//设置整个报表的标题格式

xSt.get_Range(excel.Cells[2,2],excel.Cells[2,2]).Font.Bold=true;

xSt.get_Range(excel.Cells[2,2],excel.Cells[2,2]).Font.Size=22;

//设置报表表格为最适应宽度

xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Select();

xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Columns.AutoFit();

//设置整个报表的标题为跨列居中

xSt.get_Range(excel.Cells[2,2],excel.Cells[2,colIndex]).Select();

xSt.get_Range(excel.Cells[2,2],excel.Cells[2,colIndex]).HorizontalAlignment=XlHAlign.xlHAlignCenterAcrossSelection;

//绘制边框

xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Borders.LineStyle=1;

xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,2]).Borders[XlBordersIndex.xlEdgeLeft].Weight=XlBorderWeight.xlThick;

//设置左边线加粗

xSt.get_Range(excel.Cells[4,2],excel.Cells[4,colIndex]).Borders[XlBordersIndex.xlEdgeTop].Weight=XlBorderWeight.xlThick;

//设置上边线加粗

xSt.get_Range(excel.Cells[4,colIndex],excel.Cells[rowSum,colIndex]).Borders[XlBordersIndex.xlEdgeRight].Weight=XlBorderWeight.xlThick;

//设置右边线加粗

xSt.get_Range(excel.Cells[rowSum,2],excel.Cells[rowSum,colIndex]).Borders[XlBordersIndex.xlEdgeBottom].Weight=XlBorderWeight.xlThick;

//设置下边线加粗

//显示效果

excel.Visible=true;

//xSt.Export(Server.MapPath("

."

+this.xlfile.Text+"

SheetExportActionEnum.ssExportActionNone,Microsoft.Office.Interop.OWC.SheetExportFormat.ssExportHTML);

xBk.SaveCopyAs(Server.MapPath("

ds=null;

xBk.Close(false,null,null);

excel.Quit();

System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk);

System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);

System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt);

xBk=null;

excel=null;

xSt=null;

stringpath=Server.MapPath(this.xlfile.Text+"

System.IO.FileInfofile=newSystem.IO.FileInfo(path);

Response.Charset="

//添加头信息,为"

文件下载/另存为"

对话框指定默认文件名

Response.AddHeader("

filename="

+Server.UrlEncode(file.Name));

//添加头信息,指定文件大小,让浏览器能够显示下载进度

Content-Length"

file.Length.ToString());

//指定返回的是一个不能被客户端读取的流,必须被下载

Response.ContentType="

//把文件流发送到客户端

Response.WriteFile(file.FullName);

//停止页面的执行 

Response.End();

上面的方面,均将要导出的excel数据,直接给浏览器输出文件流,下面的方法是首先将其存到服务器的某个文件夹中,然后把文件发送到客户端。

这样可以持久的把导出的文件存起来,以便实现其它功能。

5、将execl文件导出到服务器上,再下载。

二、winForm中导出Execl的方法:

1、方法1:

SqlConnectionconn=newSqlConnection(System.Configuration.ConfigurationSettings.AppSettings["

conn"

]);

SqlDataAdapterda=newSqlDataAdapter("

select*fromtb1"

conn);

DataSetds=newDataSet();

da.Fill(ds,"

table1"

DataTabledt=ds.Tables["

];

stringname=System.Configuration.ConfigurationSettings.AppSettings["

downloadurl"

].ToString()+DateTime.Today.ToString("

)+newRandom(DateTime.Now.Millisecond).Next(10000).ToString()+"

.csv"

//存放到web.config中downloadurl指定的路径,文件格式为当前日期+4位随机数

FileStreamfs=newFileStream(name,FileMode.Create,FileAccess.Write);

StreamWritersw=newStreamWriter(fs,System.Text.Encoding.GetEncoding("

gb2312"

));

sw.WriteLine("

自动编号,姓名,年龄"

foreach(DataRowdrindt.Rows)

sw.WriteLine(dr["

ID"

]+"

+dr["

vName"

iAge"

sw.Close();

+Server.UrlEncode(name));

//指定返回的是一个不能被客户端读取的流,必须被下载

Response.WriteFile(name);

publicvoidOut2Excel(stringsTableName,stringurl)

Excel.ApplicationoExcel=newExcel.Application();

WorkbooksoBooks;

WorkbookoBook;

SheetsoSheets;

WorksheetoSheet;

RangeoCells;

stringsFile="

sTemplate="

/

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > IT计算机 > 电脑基础知识

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2