简体   繁体   English

C#导出为Excel格式

[英]C# Export to Excel format

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

My question: 我的问题:

After I exported the data into an Excel file, I have values in my Excel table below, 将数据导出到Excel文件后,我的Excel表格中有值,

Exported Excel table 导出的Excel表格

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" . 如果我ctrl ”并用鼠标35.0和40.0 点击 Excel 不能求和35.0和40.0因为35.0是字符串显示“35.0”而40.0是字符串显示“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 ? 如何通过C#代码删除Excel表格中的“”字符串?

Thanks. 谢谢。

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

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 如果您不反对使用第三方库,可以使用以下解决方案: http//www.codeproject.com/Articles/692092/A-free-Export-to-Excel-Csharp-class-using-OpenXML

It uses OpenXML library (available through nuget). 它使用OpenXML库(可通过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 ...). 要查看的方法是WriteDataTableToExcelWorksheet(DataTable dt, WorksheetPart worksheetPart) ,您必须处理不同的数据类型( DoubleDecimalDateTime ...)。 I also recomend using number.ToString(CultureInfo.InvariantCulture) when getting string values of numeric columns. 在获取数字列的字符串值时,我还建议使用number.ToString(CultureInfo.InvariantCulture) 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 您还可以在列中设置数字的自定义格式(如果您确实需要使用特定的数字格式): http//lateral8.com/articles/2010/6/11/openxml-sdk-20-formatting- Excel的values.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM