C# Export to Excel format

故事扮演 提交于 2019-12-10 21:34:17

问题


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 method:

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

My question:

After I exported the data into an Excel file, I have values in my Excel table below,

Exported Excel table

Dollar

50.0

35.0

40.0

60.0


etc..

If I press to "ctrl" and click with mouse 35.0 and 40.0 Excel can not sum 35.0 and 40.0 because 35.0 is string displays "35.0" and 40.0 is string displays "40.0".

The problem is about sum selected rows however I can not sum because values are string not numeric

How can I remove " " out of string number in Excel table by C# code ?

Thanks.


回答1:


To remove the quotes change this line:

strLawTable.Append("<th style=\"text-align: right\">=\"" + GetExcellFormatString(Currency.USD) + "\"</th>");
                                                     ^^remove                                     ^^remove

to this:

strLawTable.Append("<th style=\"text-align: right\">=" + GetExcellFormatString(Currency.USD) + "</th>");



回答2:


If you're not opposed to using third party libraries, there is this solution: http://www.codeproject.com/Articles/692092/A-free-Export-to-Excel-Csharp-class-using-OpenXML

It uses OpenXML library (available through nuget).

It works like a charm - well, if there are only integers and strings in your data. It can be customized, though. The method to look at is WriteDataTableToExcelWorksheet(DataTable dt, WorksheetPart worksheetPart), where you'll have to take care of different data types (Double, Decimal, DateTime...). I also recomend using number.ToString(CultureInfo.InvariantCulture) when getting string values of numeric columns. Otherwise there will be the same problem that you have: the column won't be recognized as a correct number (at best it will be a text column, or there will be error in the cell).

You can also set up custom formatting of numbers in your column (if you really have to use specific formatting for your numbers): http://lateral8.com/articles/2010/6/11/openxml-sdk-20-formatting-excel-values.aspx



来源:https://stackoverflow.com/questions/27621609/c-sharp-export-to-excel-format

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