C#导出为Excel格式 [英] C# Export to Excel format

查看:206
本文介绍了C#导出为Excel格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的ActionResult:



  VAR strLawTable =新的StringBuilder(); 

strLawTable.Append(<&THEAD GT;);
strLawTable.Append(&所述; TR>中);

strLawTable.Append(百分位风格= \的text-align:right\>美元< /第i);

strLawTable.Append(< / TR>中);
strLawTable.Append(&下; / THEAD>中);

strLawTable.Append(<&TBODY GT;);

的foreach(在Model.List货币货币)
{
strLawTable.Append(< TR>中);

strLawTable.Append(百分位风格= \的text-align:right\> = \+ GetExcellFormatString(Currency.USD)+\< /第i);

strLawTable.Append(< / TR>中);
}

strLawTable.Append(< / TBODY>);


串headerTable =<表> + strLawTable +< /表>,

Response.Clear();
Response.AddHeader(内容处置,附件;文件名= TestFile.xls);
Response.ContentType =应用程序/ MS-Excel的;

Response.ContentEncoding = System.Text.Encoding.Unicode;
Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());

System.IO.StringWriter SW =新System.IO.StringWriter();
sw.Write(headerTable);

System.Web.UI.HtmlTextWriter HW =新的HtmlTextWriter(SW);

的Response.Write(sw.ToString());
到Response.End();



GetExcellFormatString 方法:

 公共字符串GetExcellFormatString(双doubleAmount)
{
如果(doubleAmount< 1000)
返回doubleAmount.ToString (0.00);
否则如果(doubleAmount< 1000000)
返回doubleAmount.ToString(0000.00);
否则如果(doubleAmount< 1000000000)
返回doubleAmount.ToString(0000,000.00);
否则如果(doubleAmount<万亿)
返回doubleAmount.ToString(0000000000.00);
否则返回doubleAmount.ToString();
}



我的问题:



<我导出到Excel文件中的数据p>之后,我有价值观在我的Excel表格的下方,



导出Excel表格



美元

  50.0 

35.0

40.0

60.0


等。

如果我的新闻 CTRL 点击鼠标35.0和40.0 Excel中的不能总结35.0和40.0 ,因为 35.0为字符串显示35.0和40.0是字符串显示40.0



现在的问题是关于总之选定行,但是我不能概括,因为值的字符串不是数字



如何删除,在Excel表格出串号由C#代码?



感谢。


解决方案

要删除引号变化该行:

  strLawTable.Append(百分位风格= \的text-align:right\> = \+ GetExcellFormatString(Currency.USD)+\&下; /第i); 
^^删除^^删除



这样:

  strLawTable.Append(百分位风格= \的text-align:right\> =+ GetExcellFormatString(Currency.USD)+ < /第i); 


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

这篇关于C#导出为Excel格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆