在Excel中只格式化1列以强制文本,并将所有其他文件保存为数字,同时从asp.net导出到Excel

我必须下载一个有10列的Excel文件,在Excel中,我将70696918000007视为“7.06969E + 13”,在添加时,一切都正常显示

sbResponseString.Append("<html xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns=\"http://www.w3.org/TR/REC-html40\"> <head><style> td {mso-number-format:"\@";} </style><meta http-equiv=\"Content-Type\" content=\"text/html;charset=windows-1252\"><!--[if gte mso 9]> <xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>" + worksheet_name + "</x:Name><x:WorksheetOptions><x:Panes></x:Panes></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head> <body>"); 

但我不想将所有其他列作为文本,因为我必须find其他列的总和。 所以如何格式化只有1列的Excel文本

这非常简单,我只是添加一个类到我想要转换为string的列

  DataGrid dataExportExcel = new DataGrid(); dataExportExcel.DataSource = ds.Tables[0]; dataExportExcel.DataBind(); for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { dataExportExcel.Items[i].Cells[5].Attributes.Add("class", "Invoice") } 

然后改变了代码

 sbResponseString.Append("<html xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns=\"http://www.w3.org/TR/REC-html40\"> <head><style> .Invoice {mso-number-format:\'@'}</style><meta http-equiv=\"Content-Type\" content=\"text/html;charset=windows-1252\"><!--[if gte mso 9]> <xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>" + worksheet_name + "</x:Name><x:WorksheetOptions><x:Panes></x:Panes></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head> <body>"); 

简单地为发票类添加单独的样式