C# 将数据集导出到excel时,数字将转换为文本
Iam将数据集导出到excel时,工作表上的数字在导出到excel时显示为文本 代码如下:C# 将数据集导出到excel时,数字将转换为文本,c#,asp.net,C#,Asp.net,Iam将数据集导出到excel时,工作表上的数字在导出到excel时显示为文本 代码如下: public class Excel { const int rowLimit = 65000; private static string getWorkbookTemplate() { StringBuilder sb = new StringBuilder(818); sb.AppendFormat(@"<?xml version=""
public class Excel
{
const int rowLimit = 65000;
private static string getWorkbookTemplate()
{
StringBuilder sb = new StringBuilder(818);
sb.AppendFormat(@"<?xml version=""1.0""?>{0}", Environment.NewLine);
sb.AppendFormat(@"<?mso-application progid=""Excel.Sheet""?>{0}", Environment.NewLine);
sb.AppendFormat(@"<Workbook xmlns=""urn:schemas-microsoft-com:office:spreadsheet""{0}", Environment.NewLine);
sb.AppendFormat(@" xmlns:o=""urn:schemas-microsoft-com:office:office""{0}", Environment.NewLine);
sb.AppendFormat(@" xmlns:x=""urn:schemas-microsoft-com:office:excel""{0}", Environment.NewLine);
sb.AppendFormat(@" xmlns:ss=""urn:schemas-microsoft-com:office:spreadsheet""{0}", Environment.NewLine);
sb.AppendFormat(@" xmlns:html=""http://www.w3.org/TR/REC-html40"">{0}", Environment.NewLine);
sb.AppendFormat(@" <Styles>{0}", Environment.NewLine);
sb.AppendFormat(@" <Style ss:ID=""Default"" ss:Name=""Normal"">{0}", Environment.NewLine);
sb.AppendFormat(@" <Alignment ss:Vertical=""Bottom""/>{0}", Environment.NewLine);
sb.AppendFormat(@" <Borders/>{0}", Environment.NewLine);
sb.AppendFormat(@" <Font ss:FontName=""Verdana"" x:Family=""Swiss"" ss:Size=""12"" ss:Color=""#0000A0""/>{0}", Environment.NewLine);
sb.AppendFormat(@" <Interior/>{0}", Environment.NewLine);
sb.AppendFormat(@" <NumberFormat/>{0}", Environment.NewLine);
sb.AppendFormat(@" <Protection/>{0}", Environment.NewLine);
sb.AppendFormat(@" </Style>{0}", Environment.NewLine);
sb.AppendFormat(@" <Style ss:ID=""s62"">{0}", Environment.NewLine);
sb.AppendFormat(@" <Font ss:FontName=""Calibri"" x:Family=""Swiss"" ss:Size=""11"" ss:Color=""#000000""{0}", Environment.NewLine);
sb.AppendFormat(@" ss:Bold=""1""/>{0}", Environment.NewLine);
sb.AppendFormat(@" </Style>{0}", Environment.NewLine);
sb.AppendFormat(@" <Style ss:ID=""s63"">{0}", Environment.NewLine);
sb.AppendFormat(@" <NumberFormat ss:Format=""Short Date""/>{0}", Environment.NewLine);
sb.AppendFormat(@" </Style>{0}", Environment.NewLine);
sb.AppendFormat(@" </Styles>{0}", Environment.NewLine);
sb.Append(@"{0}\r\n</Workbook>");
return sb.ToString();
}
private static string replaceXmlChar(string input)
{
input = input.Replace("&", "&");
input = input.Replace("<", "<");
input = input.Replace(">", ">");
input = input.Replace("\"", """);
input = input.Replace("'", "'");
return input;
}
private static string getCell(Type type, object cellData)
{
Object data = (cellData is DBNull) ? "" : cellData;
if (type.Name.Contains("Int") || type.Name.Contains("Double") || type.Name.Contains("Decimal")) return string.Format("<Cell><Data ss:Type=\"Number\">{0}</Data></Cell>", data);
if (type.Name.Contains("Date") && data.ToString() != string.Empty)
{
return string.Format("<Cell ss:StyleID=\"s63\"><Data ss:Type=\"DateTime\">{0}</Data></Cell>", Convert.ToDateTime(data).ToString("yyyy-MM-dd"));
}
return string.Format("<Cell><Data ss:Type=\"String\">{0}</Data></Cell>", replaceXmlChar(data.ToString()));
}
private static string getWorksheets(DataSet source)
{
StringWriter sw = new StringWriter();
if (source == null || source.Tables.Count == 0)
{
sw.Write("<Worksheet ss:Name=\"Sheet1\">\r\n<Table>\r\n<Row><Cell><Data ss:Type=\"String\"></Data></Cell></Row>\r\n</Table>\r\n</Worksheet>");
return sw.ToString();
}
foreach (DataTable dt in source.Tables)
{
if (dt.Rows.Count == 0)
sw.Write("<Worksheet ss:Name=\"" + replaceXmlChar(dt.TableName) + "\">\r\n<Table>\r\n<Row><Cell ss:StyleID=\"s62\"><Data ss:Type=\"String\"></Data></Cell></Row>\r\n</Table>\r\n</Worksheet>");
else
{
//write each row data
int sheetCount = 0;
for (int i = 0; i < dt.Rows.Count; i++)
{
if ((i % rowLimit) == 0)
{
//add close tags for previous sheet of the same data table
if ((i / rowLimit) > sheetCount)
{
sw.Write("\r\n</Table>\r\n</Worksheet>");
sheetCount = (i / rowLimit);
}
sw.Write("\r\n<Worksheet ss:Name=\"" + replaceXmlChar(dt.TableName) +
(((i / rowLimit) == 0) ? "" : Convert.ToString(i / rowLimit)) + "\">\r\n<Table>");
//write column name row
sw.Write("\r\n<Row>");
foreach (DataColumn dc in dt.Columns)
sw.Write(string.Format("<Cell ss:StyleID=\"s62\"><Data ss:Type=\"String\">{0}</Data></Cell>", replaceXmlChar(dc.ColumnName)));
sw.Write("</Row>");
}
sw.Write("\r\n<Row>");
foreach (DataColumn dc in dt.Columns)
sw.Write(getCell(dc.DataType, dt.Rows[i][dc.ColumnName]));
sw.Write("</Row>");
}
sw.Write("\r\n</Table>\r\n</Worksheet>");
}
}
return sw.ToString();
}
public static string GetExcelXml(DataTable dtInput, string filename)
{
string excelTemplate = getWorkbookTemplate();
DataSet ds = new DataSet();
ds.Tables.Add(dtInput.Copy());
string worksheets = getWorksheets(ds);
string excelXml = string.Format(excelTemplate, worksheets);
return excelXml;
}
public static string GetExcelXml(DataSet dsInput, string filename)
{
string excelTemplate = getWorkbookTemplate();
string worksheets = getWorksheets(dsInput);
string excelXml = string.Format(excelTemplate, worksheets);
return excelXml;
}
public static void ToExcel(DataSet dsInput, string filename, HttpResponse response)
{
string excelXml = GetExcelXml(dsInput, filename);
response.Clear();
response.AppendHeader("Content-Type", "application/vnd.ms-excel");
response.AppendHeader("Content-disposition", "attachment; filename=" + filename);
response.Write(excelXml);
response.Flush();
response.End();
}
public static void ToExcel(DataTable dtInput, string filename, HttpResponse response)
{
DataSet ds = new DataSet();
ds.Tables.Add(dtInput.Copy());
ToExcel(ds, filename, response);
}
}
公共类Excel
{
常数int rowLimit=65000;
私有静态字符串getWorkbookTemplate()
{
StringBuilder sb=新StringBuilder(818);
sb.AppendFormat(@“{0}”,Environment.NewLine);
sb.AppendFormat(@“{0}”,Environment.NewLine);
sb.AppendFormat(@“{0}”,Environment.NewLine);
sb.AppendFormat(@“{0}”,Environment.NewLine);
sb.AppendFormat(@“{0}”,Environment.NewLine);
sb.AppendFormat(@“{0}”,Environment.NewLine);
sb.AppendFormat(@“{0}”,Environment.NewLine);
sb.AppendFormat(@“{0}”,Environment.NewLine);
sb.AppendFormat(@“{0}”,Environment.NewLine);
sb.AppendFormat(@“{0}”,Environment.NewLine);
sb.AppendFormat(@“{0}”,Environment.NewLine);
sb.AppendFormat(@“{0}”,Environment.NewLine);
sb.AppendFormat(@“{0}”,Environment.NewLine);
sb.AppendFormat(@“{0}”,Environment.NewLine);
sb.AppendFormat(@“{0}”,Environment.NewLine);
sb.AppendFormat(@“{0}”,Environment.NewLine);
sb.AppendFormat(@“{0}”,Environment.NewLine);
sb.AppendFormat(@“{0}”,Environment.NewLine);
sb.AppendFormat(@“{0}”,Environment.NewLine);
sb.Append(@“{0}\r\n”);
使某人返回字符串();
}
私有静态字符串replaceXmlChar(字符串输入)
{
输入=输入。替换(“&”、“&”);
输入=输入。替换(“,”);
输入=输入。替换(“\”,“”);
输入=输入。替换(“”,“&apos;”);
返回输入;
}
私有静态字符串getCell(类型,对象cellData)
{
对象数据=(cellData为DBNull)?“”:cellData;
if(type.Name.Contains(“Int”)| | type.Name.Contains(“Double”)| | type.Name.Contains(“Decimal”))返回string.Format(“{0}”,数据);
if(type.Name.Contains(“Date”)&&data.ToString()!=string.Empty)
{
返回string.Format(“{0}”,Convert.ToDateTime(data.ToString)(“yyyy-MM-dd”);
}
返回string.Format(“{0}”,replaceXmlChar(data.ToString());
}
私有静态字符串getWorksheets(数据集源)
{
StringWriter sw=新的StringWriter();
if(source==null | | source.Tables.Count==0)
{
sw.Write(“\r\n\r\n\r\n\r\n”);
返回sw.ToString();
}
foreach(source.Tables中的DataTable dt)
{
如果(dt.Rows.Count==0)
sw.Write(“\r\n\r\n\r\n\r\n”);
其他的
{
//写入每行数据
int sheetCount=0;
对于(int i=0;i张数)
{
sw.Write(“\r\n\r\n”);
sheetCount=(i/rowLimit);
}
sw.Write(“\r\n\r\n”);
//写入列名行
sw.Write(“\r\n”);
foreach(数据列dc在dt.列中)
Write(string.Format(“{0}”,replaceXmlChar(dc.ColumnName));
sw.写(“”);
}
sw.Write(“\r\n”);
foreach(数据列dc在dt.列中)
sw.Write(getCell(dc.DataType,dt.Rows[i][dc.ColumnName]);
sw.写(“”);
}
sw.Write(“\r\n\r\n”);
}
}
返回sw.ToString();
}
公共静态字符串GetExcelXml(数据表dtInput,字符串文件名)
{
字符串excelTemplate=getWorkbookTemplate();
数据集ds=新数据集();
Add(dtInput.Copy());
字符串工作表=获取工作表(ds);
字符串excelXml=string.Format(excelTemplate,工作表);
返回excelXml;
}
公共静态字符串GetExcelXml(数据集dsInput,字符串文件名)
{
字符串excelTemplate=getWorkbookTemplate();
字符串工作表=获取工作表(dsInput);
字符串excelXml=string.Format(excelTemplate,工作表);
返回excelXml;
}
publicstaticvoidtoexcel(数据集dsInput、字符串文件名、HttpResponse响应)
{
字符串excelXml=GetExcelXml(dsInput,文件名);
response.Clear();
AppendHeader(“内容类型”、“应用程序/vnd.ms excel”);
response.AppendHeader(“内容处置”、“附件;文件名=“+filename”);
response.Write(excelXml);
response.Flush();
response.End();
}
publicstaticvoidtoexcel(datatabledtinput、字符串文件名、HttpResponse响应)
{
数据集ds=新数据集();
Add(dtInput.Copy());
ToExcel(ds、文件名、响应);
}
}
我不太清楚为什么,但我隐约记得很久以前在我们的项目中处理过类似的事情。我认为修复(或解决方法)是在数字列中的数字前面加上一个单引号(')。哪个if语句与getCell函数中的数值匹配?它在OpenOffice(Sun)3.0Hey Madhur中无法正确打开,我应该在哪里做这件事,在代码背后还是数据库方面我很困惑,请你清楚,谢谢,我不确定在你的代码上面的什么地方可以做这件事。但我可以告诉你需要做什么——这样你就可以知道在哪里可以做。您可能会有一些代码将“数字”/“数字”写入电子表格/工作簿中的“单元格”。在这里,如果你把数字“10”写下来,而不是把它写成“10”