如何设置 C# 生成的 Excel 电子表格的工作表名称?
我们通过向 Response 写入 HTML 表来在 C# .net 网站中生成 Excel 电子表格。这很好用,但是工作表名称设置为与文件名相同。
有没有办法设置这个?导入时我们需要特定的工作表名称。
代码:
string filename = String.Format( "{0}-CopyDataBack_{1}.xls", Master.EventID.ToString(), DateTime.Now.ToString( "dd-MM-yy_HHmm" ) );
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("content-disposition", "attachment;filename=" + filename );
Response.Charset = "";
this.EnableViewState = false;
System.Text.StringBuilder tableOut = new System.Text.StringBuilder();
// ... Create an HTML table in a StringBuilder ...
Response.Write( tableOut );
Response.End();
We generate an Excel spreadsheet in our C# .net website by writing out an HTML table to Response. This works great, but the sheet name gets set to the same as the file name.
Is there away to set this? We require a certain sheet name when importing.
Code:
string filename = String.Format( "{0}-CopyDataBack_{1}.xls", Master.EventID.ToString(), DateTime.Now.ToString( "dd-MM-yy_HHmm" ) );
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("content-disposition", "attachment;filename=" + filename );
Response.Charset = "";
this.EnableViewState = false;
System.Text.StringBuilder tableOut = new System.Text.StringBuilder();
// ... Create an HTML table in a StringBuilder ...
Response.Write( tableOut );
Response.End();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过创建表并将内容类型设置为 Excel,您无法定义工作表名称,也无法创建更多工作表。
您可以使用 OpenXML SDK,但您的客户端需要使用 适用于 Word、Excel 和 PowerPoint 2007 文件格式的 Microsoft Office 兼容包。
By creating a table and setting content-type to Excel you can't define a worksheet name, nor to create more worksheets.
You can to generate your complete Excel workbook by using OpenXML SDK, but your client will need to handle Office 2007 file formats by using Microsoft Office Compatibility Pack for Word, Excel, and PowerPoint 2007 File Formats.