简体   繁体   中英

C# Export to Excel format

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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