C#导出为excell
的ActionResult:
var strLawTable = new StringBuilder(); strLawTable.Append("<thead>"); strLawTable.Append("<tr>"); strLawTable.Append("<th style=\"text-align: right\">Dollar</th>"); strLawTable.Append("</tr>"); strLawTable.Append("</thead>"); strLawTable.Append("<tbody>"); foreach (Currency currency in Model.List) { strLawTable.Append("<tr>"); strLawTable.Append("<th style=\"text-align: right\">" + GetExcellFormatString(Currency.USD) + "</th>"); strLawTable.Append("</tr>"); } strLawTable.Append("</tbody>"); string headerTable = "<table>" + strLawTable + "</table>"; Response.Clear(); Response.AddHeader("content-disposition", "attachment;filename=TestFile.xls"); Response.ContentType = "application/ms-excel"; Response.ContentEncoding = System.Text.Encoding.Unicode; Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble()); System.IO.StringWriter sw = new System.IO.StringWriter(); sw.Write(headerTable); System.Web.UI.HtmlTextWriter hw = new HtmlTextWriter(sw); Response.Write(sw.ToString()); Response.End();
GetExcellFormatString
方法:
public string GetExcellFormatString(double doubleAmount) { if (doubleAmount < 1000) return doubleAmount.ToString("0.00"); else if (doubleAmount < 1000000) return doubleAmount.ToString("0000.00"); else if (doubleAmount < 1000000000) return doubleAmount.ToString("0000,000.00"); else if (doubleAmount < 1000000000000) return doubleAmount.ToString("0000000000.00"); else return doubleAmount.ToString(); }
我的问题:
我的客户改变了Windows的区域设置,他们看到了
3.50 as "May50" 20.50 as "Apr15" etc..
Excel格式在我的电脑上是正确的,但是在客户的电脑上它总是显示date文本。
我也在下面尝试,但仍然是同样的问题
byte[] fileContents = Encoding.UTF8.GetBytes(headerTable); return File(fileContents, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Test.xls");
我尝试了很多解决scheme(返回文件等),我错过了什么,我需要添加什么?
我可以使用return file
或ActionResult
任何东西,我只需要解决scheme。
谢谢。
改变这一行
strLawTable.Append("<th style=\"text-align: right\">" + GetExcellFormatString(Currency.USD) + "</th>");
如:
strLawTable.Append("<th style=\"text-align: right\">=\"" + GetExcellFormatString(Currency.USD) + "\"</th>");
请注意<\ th>之前和之后的“\”和“\”
- jqgrid + EF + MVC:如何在Excel中导出? 你build议哪种方法?
- FormView到Excel – 如何从模板中的控件拉出数据放置在单元格中
- Excel崩溃并错误地显示使用Apache POI编写的文件(Excel 87-2003 .XLS格式)
- 如何将“date相似”文本列导出到csv
- 导出GridView到Excel#2
- ASP.NET导出到Excel显示电子表格中的HTML标记
- SQL Server 2005 SSIS数据传输SQL到Excel损坏Excel文件
- 如何导出报告服务报告以优于Excel列中的embedded式计算?
- 无法同时使用readxl :: read_excel和xlsx :: write.xlsx