C#导出生成excel文件的方法小结(xml,html方式)
时间:2023-10-03 16:32:26
直接贴上代码,里面都有注释
/// <summary>
/// xml格式生成excel文件并存盘;
/// </summary>
/// <param name="page">生成报表的页面,没有传null</param>
/// <param name="dt">数据表</param>
/// <param name="TableTitle">报表标题,sheet1名</param>
/// <param name="fileName">存盘文件名,全路径</param>
/// <param name="IsDown">生成文件后是否提示下载,只有web下才有效</param>
public static void CreateExcelByXml(System.Web.UI.Page page, DataTable dt, String TableTitle, string fileName, bool IsDown)
{
StringBuilder strb = new StringBuilder();
strb.Append(" <html xmlns:o=\"urn:schemas-microsoft-com:office:office\"");
strb.Append("xmlns:x=\"urn:schemas-microsoft-com:office:excel\"");
strb.Append("xmlns=\"");
strb.Append(" <head> <meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>");
strb.Append(" <style>");
strb.Append("body");
strb.Append(" {mso-style-parent:style0;");
strb.Append(" font-family:\"Times New Roman\", serif;");
strb.Append(" mso-font-charset:0;");
strb.Append(" mso-number-format:\"@\";}");
strb.Append("table");
//strb.Append(" {border-collapse:collapse;margin:1em 0;line-height:20px;font-size:12px;color:#222; margin:0px;}");
strb.Append(" {border-collapse:collapse;margin:1em 0;line-height:20px;color:#222; margin:0px;}");
strb.Append("thead tr td");
strb.Append(" {background-color:#e3e6ea;color:#6e6e6e;text-align:center;font-size:14px;}");
strb.Append("tbody tr td");
strb.Append(" {font-size:12px;color:#666;}");
strb.Append(" </style>");
strb.Append(" <xml>");
strb.Append(" <x:ExcelWorkbook>");
strb.Append(" <x:ExcelWorksheets>");
strb.Append(" <x:ExcelWorksheet>");
//设置工作表 sheet1的名称
strb.Append(" <x:Name>" + TableTitle + " </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> ");
strb.Append(" <table style=\"border-right: 1px solid #CCC;border-bottom: 1px solid #CCC;text-align:center;\"> <thead><tr>");
//合格所有列并显示标题
strb.Append(" <td style=\"text-align:center;background:#d3eeee;font-size:18px;\" colspan=\"" + dt.Columns.Count + "\" ><b>");
strb.Append(TableTitle);
strb.Append(" </b></td> ");
strb.Append(" </tr>");
strb.Append(" </thead><tbody><tr style=\"height:20px;\">");
if (dt != null)
{
//写列标题
int columncount = dt.Columns.Count;
for (int columi = 0; columi < columncount; columi++)
{
strb.Append(" <td style=\"width:110px;;text-align:center;background:#CCC;\"> <b>" + dt.Columns[columi] + " </b> </td>");
}
strb.Append(" </tr>");
//写数据
for (int i = 0; i < dt.Rows.Count; i++)
{
strb.Append(" <tr style=\"height:20px;\">");
for (int j = 0; j < dt.Columns.Count; j++)
{
strb.Append(" <td style=\"width:110px;;text-align:center;\">" + dt.Rows[i][j].ToString() + " </td>");
}
strb.Append(" </tr>");
}
}
strb.Append(" </tbody> </table>");
strb.Append(" </body> </html>");
string ExcelFileName = fileName;
//string ExcelFileName = Path.Combine(page.Request.PhysicalApplicationPath, path+"/guestData.xls");
//报表文件存在则先删除
if (File.Exists(ExcelFileName))
{
File.Delete(ExcelFileName);
}
StreamWriter writer = new StreamWriter(ExcelFileName, false);
writer.WriteLine(strb.ToString());
writer.Close();
//如果需下载则提示下载对话框
if (IsDown)
{
DownloadExcelFile(page, ExcelFileName);
}
}
---------
/// <summary>
/// web下提示下载
/// </summary>
/// <param name="page"></param>
/// <param name="filename">文件名,全路径</param>
public static void DownloadExcelFile(System.Web.UI.Page page, string FileName)
{
page.Response.Write("path:" + FileName);
if (!System.IO.File.Exists(FileName))
{
MessageBox.ShowAndRedirect(page, "文件不存在!", FileName);
}
else
{
FileInfo f = new FileInfo(FileName);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + f.Name);
HttpContext.Current.Response.AddHeader("Content-Length", f.Length.ToString());
HttpContext.Current.Response.AddHeader("Content-Transfer-Encoding", "binary");
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.WriteFile(f.FullName);
HttpContext.Current.Response.End();
}
}
需要cs类文件的可以去下载 点击下载
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#实现的简单验证码识别实例
![](https://img.aspxhome.com/file/2023/0/82240_0s.jpg)
springMVC+ajax实现文件上传且带进度条实例
C++找出字符串中出现最多的字符和次数,时间复杂度小于O(n^2)
c# 用Base64实现文件上传
详细解读Java的Lambda表达式
![](https://img.aspxhome.com/file/2023/0/71650_0s.jpg)
Java后端学习精华之TCP通信传输协议详解
![](https://img.aspxhome.com/file/2023/1/64221_0s.png)
Android控件之ListView用法实例详解
![](https://img.aspxhome.com/file/2023/0/90130_0s.png)
详解Android应用开发中Intent的作用及使用方法
C#实现会移动的文字效果
![](https://img.aspxhome.com/file/2023/5/111225_0s.jpg)
详解IDEA使用Maven项目不能加入本地Jar包的解决方法
![](https://img.aspxhome.com/file/2023/4/108414_0s.png)
深入解析Java的Hibernate框架中的一对一关联映射
C# 7.2中结构体性能问题的解决方案
Android编程使用WebView实现与Javascript交互的方法【相互调用参数、传值】
![](https://img.aspxhome.com/file/2023/8/138238_0s.png)
SpringBoot @Cacheable自定义KeyGenerator方式
![](https://img.aspxhome.com/file/2023/9/88069_0s.png)
详解Java高级特性之反射
C#中用foreach语句遍历数组及将数组作为参数的用法
Springboot中如何使用Redisson实现分布式锁浅析
springboot Interceptor拦截器excludePathPatterns忽略失效
![](https://img.aspxhome.com/file/2023/9/74879_0s.jpg)
Android帧动画、补间动画、属性动画用法详解
Android仿微信菜单(Menu)(使用C#和Java分别实现)
![](https://img.aspxhome.com/file/2023/3/111673_0s.gif)