在c#中将dataset输出成xml内容的xls文件,解决其它输出不能在excel中显示“001”的前导0的问题
2007-09-14 17:16
1266 查看
记得以前直接把gridview导出称excel,虽然很简单,但是却不能控制具体细节,尤其是字符串的“00001”到了excel中必定变为整数1,一直没找到解决办法,直到在codeproject上找到这个函数,它直接把数据写称excel可以读取的xml文档,仅仅把扩展名改为xls,但是效果却很好。
public static void DataSetExportToExcel(DataSet source, string fileName)
...{
System.IO.StreamWriter excelDoc;
excelDoc = new System.IO.StreamWriter(fileName);
const string startExcelXML = "<xml version> <Workbook " +
"xmlns="urn:schemas-microsoft-com:office:spreadsheet" " +
" xmlns:o="urn:schemas-microsoft-com:office:office" " +
"xmlns:x="urn:schemas- microsoft-com:office:" +
"excel" xmlns:ss="urn:schemas-microsoft-com:" +
"office:spreadsheet"> <Styles> " +
"<Style ss:ID="Default" ss:Name="Normal"> " +
"<Alignment ss:Vertical="Bottom"/> <Borders/>" +
" <Font/> <Interior/> <NumberFormat/>" +
" <Protection/> </Style> " +
"<Style ss:ID="BoldColumn"> <Font " +
"x:Family="Swiss" ss:Bold="1"/> </Style> " +
"<Style ss:ID="StringLiteral"> <NumberFormat" +
" ss:Format="@"/> </Style> <Style " +
"ss:ID="Decimal"> <NumberFormat " +
"ss:Format="0.0000"/> </Style> " +
"<Style ss:ID="Integer"> <NumberFormat " +
"ss:Format="0"/> </Style> <Style " +
"ss:ID="DateLiteral"> <NumberFormat " +
"ss:Format="mm/dd/yyyy;@"/> </Style> " +
"</Styles> ";
const string endExcelXML = "</Workbook>";
int rowCount = 0;
int sheetCount = 1;
/**//*
<xml version>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
<Styles>
<Style ss:ID="Default" ss:Name="Normal">
<Alignment ss:Vertical="Bottom"/>
<Borders/>
<Font/>
<Interior/>
<NumberFormat/>
<Protection/>
</Style>
<Style ss:ID="BoldColumn">
<Font x:Family="Swiss" ss:Bold="1"/>
</Style>
<Style ss:ID="StringLiteral">
<NumberFormat ss:Format="@"/>
</Style>
<Style ss:ID="Decimal">
<NumberFormat ss:Format="0.0000"/>
</Style>
<Style ss:ID="Integer">
<NumberFormat ss:Format="0"/>
</Style>
<Style ss:ID="DateLiteral">
<NumberFormat ss:Format="mm/dd/yyyy;@"/>
</Style>
</Styles>
<Worksheet ss:Name="Sheet1">
</Worksheet>
</Workbook>
*/
excelDoc.Write(startExcelXML);
excelDoc.Write("<Worksheet ss:Name="Sheet" + sheetCount + "">");
excelDoc.Write("<Table>");
excelDoc.Write("<Row>");
for (int x = 0; x < source.Tables[0].Columns.Count; x++)
...{
excelDoc.Write("<Cell ss:StyleID="BoldColumn"><Data ss:Type="String">");
excelDoc.Write(source.Tables[0].Columns[x].ColumnName);
excelDoc.Write("</Data></Cell>");
}
excelDoc.Write("</Row>");
foreach (DataRow x in source.Tables[0].Rows)
...{
rowCount++;
//if the number of rows is > 64000 create a new page to continue output
if (rowCount == 64000)
...{
rowCount = 0;
sheetCount++;
excelDoc.Write("</Table>");
excelDoc.Write(" </Worksheet>");
excelDoc.Write("<Worksheet ss:Name="Sheet" + sheetCount + "">");
excelDoc.Write("<Table>");
}
excelDoc.Write("<Row>"); //ID=" + rowCount + "
for (int y = 0; y < source.Tables[0].Columns.Count; y++)
...{
System.Type rowType;
rowType = x[y].GetType();
switch (rowType.ToString())
...{
case "System.String":
string XMLstring = x[y].ToString();
XMLstring = XMLstring.Trim();
XMLstring = XMLstring.Replace("&", "&");
XMLstring = XMLstring.Replace(">", ">");
XMLstring = XMLstring.Replace("<", "<");
excelDoc.Write("<Cell ss:StyleID="StringLiteral">" +
"<Data ss:Type="String">");
excelDoc.Write(XMLstring);
excelDoc.Write("</Data></Cell>");
break;
case "System.DateTime":
//Excel has a specific Date Format of YYYY-MM-DD followed by
//the letter 'T' then hh:mm:sss.lll Example 2005-01-31T24:01:21.000
//The Following Code puts the date stored in XMLDate
//to the format above
DateTime XMLDate = (DateTime)x[y];
string XMLDatetoString = ""; //Excel Converted Date
XMLDatetoString = XMLDate.Year.ToString() +
"-" +
(XMLDate.Month < 10 ? "0" +
XMLDate.Month.ToString() : XMLDate.Month.ToString()) +
"-" +
(XMLDate.Day < 10 ? "0" +
XMLDate.Day.ToString() : XMLDate.Day.ToString()) +
"T" +
(XMLDate.Hour < 10 ? "0" +
XMLDate.Hour.ToString() : XMLDate.Hour.ToString()) +
":" +
(XMLDate.Minute < 10 ? "0" +
XMLDate.Minute.ToString() : XMLDate.Minute.ToString()) +
":" +
(XMLDate.Second < 10 ? "0" +
XMLDate.Second.ToString() : XMLDate.Second.ToString()) +
".000";
excelDoc.Write("<Cell ss:StyleID="DateLiteral">" +
"<Data ss:Type="DateTime">");
excelDoc.Write(XMLDatetoString);
excelDoc.Write("</Data></Cell>");
break;
case "System.Boolean":
excelDoc.Write("<Cell ss:StyleID="StringLiteral">" +
"<Data ss:Type="String">");
excelDoc.Write(x[y].ToString());
excelDoc.Write("</Data></Cell>");
break;
case "System.Int16":
case "System.Int32":
case "System.Int64":
case "System.Byte":
excelDoc.Write("<Cell ss:StyleID="Integer">" +
"<Data ss:Type="Number">");
excelDoc.Write(x[y].ToString());
excelDoc.Write("</Data></Cell>");
break;
case "System.Decimal":
case "System.Double":
excelDoc.Write("<Cell ss:StyleID="Decimal">" +
"<Data ss:Type="Number">");
excelDoc.Write(x[y].ToString());
excelDoc.Write("</Data></Cell>");
break;
case "System.DBNull":
excelDoc.Write("<Cell ss:StyleID="StringLiteral">" +
"<Data ss:Type="String">");
excelDoc.Write("");
excelDoc.Write("</Data></Cell>");
break;
default:
throw (new Exception(rowType.ToString() + " not handled."));
}
}
excelDoc.Write("</Row>");
}
excelDoc.Write("</Table>");
excelDoc.Write(" </Worksheet>");
excelDoc.Write(endExcelXML);
excelDoc.Close();
}
public static void DataSetExportToExcel(DataSet source, string fileName)
...{
System.IO.StreamWriter excelDoc;
excelDoc = new System.IO.StreamWriter(fileName);
const string startExcelXML = "<xml version> <Workbook " +
"xmlns="urn:schemas-microsoft-com:office:spreadsheet" " +
" xmlns:o="urn:schemas-microsoft-com:office:office" " +
"xmlns:x="urn:schemas- microsoft-com:office:" +
"excel" xmlns:ss="urn:schemas-microsoft-com:" +
"office:spreadsheet"> <Styles> " +
"<Style ss:ID="Default" ss:Name="Normal"> " +
"<Alignment ss:Vertical="Bottom"/> <Borders/>" +
" <Font/> <Interior/> <NumberFormat/>" +
" <Protection/> </Style> " +
"<Style ss:ID="BoldColumn"> <Font " +
"x:Family="Swiss" ss:Bold="1"/> </Style> " +
"<Style ss:ID="StringLiteral"> <NumberFormat" +
" ss:Format="@"/> </Style> <Style " +
"ss:ID="Decimal"> <NumberFormat " +
"ss:Format="0.0000"/> </Style> " +
"<Style ss:ID="Integer"> <NumberFormat " +
"ss:Format="0"/> </Style> <Style " +
"ss:ID="DateLiteral"> <NumberFormat " +
"ss:Format="mm/dd/yyyy;@"/> </Style> " +
"</Styles> ";
const string endExcelXML = "</Workbook>";
int rowCount = 0;
int sheetCount = 1;
/**//*
<xml version>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
<Styles>
<Style ss:ID="Default" ss:Name="Normal">
<Alignment ss:Vertical="Bottom"/>
<Borders/>
<Font/>
<Interior/>
<NumberFormat/>
<Protection/>
</Style>
<Style ss:ID="BoldColumn">
<Font x:Family="Swiss" ss:Bold="1"/>
</Style>
<Style ss:ID="StringLiteral">
<NumberFormat ss:Format="@"/>
</Style>
<Style ss:ID="Decimal">
<NumberFormat ss:Format="0.0000"/>
</Style>
<Style ss:ID="Integer">
<NumberFormat ss:Format="0"/>
</Style>
<Style ss:ID="DateLiteral">
<NumberFormat ss:Format="mm/dd/yyyy;@"/>
</Style>
</Styles>
<Worksheet ss:Name="Sheet1">
</Worksheet>
</Workbook>
*/
excelDoc.Write(startExcelXML);
excelDoc.Write("<Worksheet ss:Name="Sheet" + sheetCount + "">");
excelDoc.Write("<Table>");
excelDoc.Write("<Row>");
for (int x = 0; x < source.Tables[0].Columns.Count; x++)
...{
excelDoc.Write("<Cell ss:StyleID="BoldColumn"><Data ss:Type="String">");
excelDoc.Write(source.Tables[0].Columns[x].ColumnName);
excelDoc.Write("</Data></Cell>");
}
excelDoc.Write("</Row>");
foreach (DataRow x in source.Tables[0].Rows)
...{
rowCount++;
//if the number of rows is > 64000 create a new page to continue output
if (rowCount == 64000)
...{
rowCount = 0;
sheetCount++;
excelDoc.Write("</Table>");
excelDoc.Write(" </Worksheet>");
excelDoc.Write("<Worksheet ss:Name="Sheet" + sheetCount + "">");
excelDoc.Write("<Table>");
}
excelDoc.Write("<Row>"); //ID=" + rowCount + "
for (int y = 0; y < source.Tables[0].Columns.Count; y++)
...{
System.Type rowType;
rowType = x[y].GetType();
switch (rowType.ToString())
...{
case "System.String":
string XMLstring = x[y].ToString();
XMLstring = XMLstring.Trim();
XMLstring = XMLstring.Replace("&", "&");
XMLstring = XMLstring.Replace(">", ">");
XMLstring = XMLstring.Replace("<", "<");
excelDoc.Write("<Cell ss:StyleID="StringLiteral">" +
"<Data ss:Type="String">");
excelDoc.Write(XMLstring);
excelDoc.Write("</Data></Cell>");
break;
case "System.DateTime":
//Excel has a specific Date Format of YYYY-MM-DD followed by
//the letter 'T' then hh:mm:sss.lll Example 2005-01-31T24:01:21.000
//The Following Code puts the date stored in XMLDate
//to the format above
DateTime XMLDate = (DateTime)x[y];
string XMLDatetoString = ""; //Excel Converted Date
XMLDatetoString = XMLDate.Year.ToString() +
"-" +
(XMLDate.Month < 10 ? "0" +
XMLDate.Month.ToString() : XMLDate.Month.ToString()) +
"-" +
(XMLDate.Day < 10 ? "0" +
XMLDate.Day.ToString() : XMLDate.Day.ToString()) +
"T" +
(XMLDate.Hour < 10 ? "0" +
XMLDate.Hour.ToString() : XMLDate.Hour.ToString()) +
":" +
(XMLDate.Minute < 10 ? "0" +
XMLDate.Minute.ToString() : XMLDate.Minute.ToString()) +
":" +
(XMLDate.Second < 10 ? "0" +
XMLDate.Second.ToString() : XMLDate.Second.ToString()) +
".000";
excelDoc.Write("<Cell ss:StyleID="DateLiteral">" +
"<Data ss:Type="DateTime">");
excelDoc.Write(XMLDatetoString);
excelDoc.Write("</Data></Cell>");
break;
case "System.Boolean":
excelDoc.Write("<Cell ss:StyleID="StringLiteral">" +
"<Data ss:Type="String">");
excelDoc.Write(x[y].ToString());
excelDoc.Write("</Data></Cell>");
break;
case "System.Int16":
case "System.Int32":
case "System.Int64":
case "System.Byte":
excelDoc.Write("<Cell ss:StyleID="Integer">" +
"<Data ss:Type="Number">");
excelDoc.Write(x[y].ToString());
excelDoc.Write("</Data></Cell>");
break;
case "System.Decimal":
case "System.Double":
excelDoc.Write("<Cell ss:StyleID="Decimal">" +
"<Data ss:Type="Number">");
excelDoc.Write(x[y].ToString());
excelDoc.Write("</Data></Cell>");
break;
case "System.DBNull":
excelDoc.Write("<Cell ss:StyleID="StringLiteral">" +
"<Data ss:Type="String">");
excelDoc.Write("");
excelDoc.Write("</Data></Cell>");
break;
default:
throw (new Exception(rowType.ToString() + " not handled."));
}
}
excelDoc.Write("</Row>");
}
excelDoc.Write("</Table>");
excelDoc.Write(" </Worksheet>");
excelDoc.Write(endExcelXML);
excelDoc.Close();
}
相关文章推荐
- 解决 Mac Pro 用 Excel 打开 CSV 文件不能正常显示的问题
- C#技巧【调用线程无法访问此对象,因为另一个线程拥有该对象的问题的解决办法】【C#读写EXCEL源码提示“office检测到此文件存在一个问题。为帮助保护您的计算机,不能打开此文件”的解决】
- C#读写EXCEL源码提示“office检测到此文件存在一个问题。为帮助保护您的计算机,不能打开此文件。 ”的解决
- 解决外部引用的js文件不能获取服务端组件的ClientID问题(c#)
- 打开chm文件不能显示内容的解决办法
- 关于Excel不能显示加载项的问题解决
- z-index解决弹出层遮罩层覆盖子div不能显示输出的问题
- Java IO流将数据输出到.csv文件中,excel打开时乱码问题的解决
- C# 导入excel数据,解决关闭excel后不能释放资源的问题
- AndroidStudio下使用 RecyclerView xml文件不显示预览条目并报错类似:NoClassDefFoundError 问题解决
- [转]C# winform中不规则窗体制作的解决方案(已经解决24位色以上不能正常显示问题)
- 解决"显示所有文件和文件夹"不能显示隐藏文件的问题!
- C# winform中不规则窗体制作的解决方案(已经解决24位色以上不能正常显示问题)
- 解决 DOS下cmd命令界面 内容不能完全显示问题
- chm文件不能显示内容的解决办法
- 解决Excel转CSV格式中文不能正常显示的问题
- 解决 qtcreator编辑*.ui文件时右键菜单不能正常显示但的问题
- 解决Logback生成的日志文件不能显示中文的问题
- 解决JasperReport生成PDF文件,中文不能显示的问题
- C#不规则窗体制作(已经解决24位色以上不能正常显示问题)