How to set the sheet name for a C# generated Excel spreadsheet?

夙愿已清 提交于 2019-12-24 20:03:22

问题


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();

回答1:


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.



来源:https://stackoverflow.com/questions/1803511/how-to-set-the-sheet-name-for-a-c-sharp-generated-excel-spreadsheet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!