手机
当前位置:查字典教程网 >编程开发 >C#教程 >C#导出生成excel文件的方法小结(xml,html方式)
C#导出生成excel文件的方法小结(xml,html方式)
摘要:直接贴上代码,里面都有注释复制代码代码如下://////xml格式生成excel文件并存盘;//////生成报表的页面,没有传null///...

直接贴上代码,里面都有注释

复制代码 代码如下:

/// <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> <thead><tr>");

//合格所有列并显示标题

strb.Append(" <td colspan="" + dt.Columns.Count + "" ><b>");

strb.Append(TableTitle);

strb.Append(" </b></td> ");

strb.Append(" </tr>");

strb.Append(" </thead><tbody><tr>");

if (dt != null)

{

//写列标题

int columncount = dt.Columns.Count;

for (int columi = 0; columi < columncount; columi++)

{

strb.Append(" <td> <b>" + dt.Columns[columi] + " </b> </td>");

}

strb.Append(" </tr>");

//写数据

for (int i = 0; i < dt.Rows.Count; i++)

{

strb.Append(" <tr>");

for (int j = 0; j < dt.Columns.Count; j++)

{

strb.Append(" <td>" + 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类文件的可以去下载 点击下载

【C#导出生成excel文件的方法小结(xml,html方式)】相关文章:

C#中读写INI文件的方法例子

c#给图片添加文字的代码小结

简介C#读取XML的两种方式

C#中使用split分割字符串的几种方法小结

C#定位txt指定行的方法小例子

C#实现窗体淡入淡出效果的方法总结

C#调用Java类的实现方法

C#删除文件目录或文件的解决方法

C#中将ListView中数据导出到Excel的实例方法

C#操作config文件的具体方法

精品推荐
分类导航