当我有一个从 SQL 表读取数据并从中生成 EXCEL 报告的 ASP.NET C# 代码/程序时,如何向 EXCEL/CSV 单元格添加额外的单引号字符?所以产生的数字不是科学价值,它会是准确的(它是一个额外的单引号字符到将有数据的单元格)。我需要在数据之前添加单引号,但是添加后,单引号本身不会显示,而是会显示确切的数字。
问题在下图中,从 SQL Result 来看,它是一个准确的数字,但是当使用代码时,它会被四舍五入到科学值。图片: http ://photoadder.com/show-image.php?id=0ca30b7f4ba55fe359c889af62a278a4 (点击放大)
当我将单引号输入到我自己的 excel 中时,这就是结果。图片:http ://photoadder.com/show-image.php?id=48366a8093afc8555bcf7c990fc3648e
这是我的部分代码
public DataTable GetData()
{
var dataTable = new DataTable();
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString);
conn.Open();
string query = "select * from table abc";
SqlCommand cmd = new SqlCommand(query, conn);
DataTable t1 = new DataTable();
using (SqlDataAdapter a = new SqlDataAdapter(cmd))
{
a.Fill(t1);
}
return t1;
}
#endregion
protected void btnGenerateReport_Click(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine("PLaccount system on click");
var dataTable = GetData();
StringBuilder builder = new StringBuilder();
List<string> columnNames = new List<string>();
List<string> rows = new List<string>();
foreach (DataColumn column in dataTable.Columns)
{
columnNames.Add(column.ColumnName);
if (column.ColumnName.ToString() == "PL_ACCOUNT_NUMBER")
{
System.Diagnostics.Debug.WriteLine("PLaccount system match");
}
}
builder.Append(string.Join(",", columnNames.ToArray())).Append("\n");
foreach (DataRow row in dataTable.Rows)
{
List<string> currentRow = new List<string>();
foreach (DataColumn column in dataTable.Columns)
{
object item = row[column];
currentRow.Add(item.ToString());
}
rows.Add(string.Join(",", currentRow.ToArray()));
}
builder.Append(string.Join("\n", rows.ToArray()));
Response.Clear();
Response.ContentType = "text/csv";
Response.AddHeader("Content-Disposition", "attachment;filename=myfilename.csv");
Response.Write(builder.ToString());
Response.End();