导出数据到Excel方法(web 不需要安装office)
2011-10-25 08:55
344 查看
///<summary>
///导出数据到Excel方法
///</summary>
///<paramname="ds">要导出的数据集合</param>
///<paramname="fileName">导出的文件名称</param>
protectedvoidCreateExcel(DataTabledt,stringfileName)
{
StringBuilderstrb=newStringBuilder();
strb.Append("<htmlxmlns:o=\"urn:schemas-microsoft-com:office:office\"");
strb.Append("xmlns:x=\"urn:schemas-microsoft-com:office:excel\"");
strb.Append("xmlns=\"http://www.w3.org/TR/REC-html40\">");
strb.Append("<head><metahttp-equiv='Content-Type'content='text/html;charset=gb2312'>");
strb.Append("<style>");
strb.Append(".xl26");
strb.Append("{mso-style-parent:style0;");
strb.Append("font-family:\"TimesNewRoman\",serif;");
strb.Append("mso-font-charset:0;");
strb.Append("mso-number-format:\"@\";}");
strb.Append("</style>");
strb.Append("<xml>");
strb.Append("<x:ExcelWorkbook>");
strb.Append("<x:ExcelWorksheets>");
strb.Append("<x:ExcelWorksheet>");
strb.Append("<x:Name>SheetAssets</x:Name>");
strb.Append("<x:WorksheetOptions>");
strb.Append("<x:DefaultRowHeight>285</x:DefaultRowHeight>");
strb.Append("<x:Selected/>");
strb.Append("<x:Panes>");
strb.Append("<x:Pane>");
strb.Append("<x:Number>3</x:Number>");
strb.Append("<x:ActiveCol>1</x:ActiveCol>");
strb.Append("</x:Pane>");
strb.Append("</x:Panes>");
strb.Append("<x:ProtectContents>False</x:ProtectContents>");
strb.Append("<x:ProtectObjects>False</x:ProtectObjects>");
strb.Append("<x:ProtectScenarios>False</x:ProtectScenarios>");
strb.Append("</x:WorksheetOptions>");
strb.Append("</x:ExcelWorksheet>");
strb.Append("<x:WindowHeight>6750</x:WindowHeight>");
strb.Append("<x:WindowWidth>10620</x:WindowWidth>");
strb.Append("<x:WindowTopX>480</x:WindowTopX>");
strb.Append("<x:WindowTopY>75</x:WindowTopY>");
strb.Append("<x:ProtectStructure>False</x:ProtectStructure>");
strb.Append("<x:ProtectWindows>False</x:ProtectWindows>");
strb.Append("</x:ExcelWorkbook>");
strb.Append("</xml>");
strb.Append("");
strb.Append("</head><body><tablealign=\"center\"style='border-collapse:collapse;table-layout:fixed'><tr>");
if(dt.Rows.Count>0)
{
//写列标题
intcolumncount=dt.Columns.Count;
for(intcolumi=0;columi<columncount;columi++)
{
strb.Append("<tdstyle='text-align:center;'><b>"+dt.Columns[columi]+"</b></td>");
}
strb.Append("</tr>");
//写数据
for(inti=0;i<dt.Rows.Count;i++)
{
strb.Append("<tr>");
for(intj=0;j<dt.Columns.Count;j++)
{
if(dt.Columns[j].DataType==Type.GetType("System.Decimal"))
{
strb.Append("<tdclass='xl26'style='text-align:right;'>"+dt.Rows[i][j].ToString()+"</td>");
}
else
{
strb.Append("<tdclass='xl26'>"+dt.Rows[i][j].ToString()+"</td>");
}
}
strb.Append("</tr>");
}
}
strb.Append("</table></body></html>");
Response.Clear();
Response.Buffer=true;
Response.Charset="GB2312";
Response.AddHeader("Content-Disposition","attachment;filename="+System.Web.HttpUtility.UrlEncode(fileName,System.Text.Encoding.UTF8)+".xls");
Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
Response.ContentType="application/ms-excel";//设置输出文件类型为excel文件。
this.EnableViewState=false;
Response.Write(strb);
Response.End();
}
相关文章推荐
- 不需要安装office excel,导出GridView所有数据为Excel
- 将dataset中的数据导出至Excel中而不需要安装MS Excel的方法(含UNICODE支持)
- 不需要安装office excel,导出GridView所有数据为Excel
- 导出数据到Excel表(不需要本地安装Office)
- 系统不安装Office导出Excel的实现方法
- .net导出数据到Excel (安装office excel环境情况)
- web端导出excel数据供用户下载的方法。
- 从Infragistics UltraWebGrid 导出数据到Excel
- C#数据导出到Excel方法汇总
- WEB表格导出为EXCEL文档的方法
- VC导出CListCtrl数据到Excel两种方法
- 数据导出到excel文件给客户端下载的几种方法
- Delphi数据导出到EXCEL方法
- ASP导出Excel数据的3种方法
- datagrid数据导出到excel文件给客户端下载的几种方法
- sql大容量数据导出到Excel(单文件多sheet方法,适合数据不超过20万)
- asp中把数据导出为excel的几种方法
- C#导入导出Excel数据的两种方法
- C#导出数据到Excel的几种方法
- matlab2013a中读取Microsoft Office Excel中数据的方法?