日期:2014-05-19  浏览次数:20867 次

操作系统相异,导出EXCEL格式相异,寻解决~~
小弟进行EXCEL导出操作,却发现操作系统不一样,导出EXCEL格式也不一样。

PS:编程环境在XP下,XP下导出一切正常。2000下格式不一样。

哪位大侠遇到过此问题,请指教!


------解决方案--------------------
楼主用下面这段代码试试:

public static void ExportToExcel(DataGridView dataGridview1, string fileName)
{
System.IO.StreamWriter excelDoc;

excelDoc = new System.IO.StreamWriter(fileName);
const string startExcelXML = " <xml version> \r\n <Workbook " +
"xmlns=\ "urn:schemas-microsoft-com:office:spreadsheet\ "\r\n " +
" xmlns:o=\ "urn:schemas-microsoft-com:office:office\ "\r\n " +
"xmlns:x=\ "urn:schemas- microsoft-com:office: " +
"excel\ "\r\n xmlns:ss=\ "urn:schemas-microsoft-com: " +
"office:spreadsheet\ "> \r\n <Styles> \r\n " +
" <Style ss:ID=\ "Default\ " ss:Name=\ "Normal\ "> \r\n " +
" <Alignment ss:Vertical=\ "Bottom\ "/> \r\n <Borders/> " +
"\r\n <Font/> \r\n <Interior/> \r\n <NumberFormat/> " +
"\r\n <Protection/> \r\n </Style> \r\n " +
" <Style ss:ID=\ "BoldColumn\ "> \r\n <Font " +
"x:Family=\ "Swiss\ " ss:Bold=\ "1\ "/> \r\n </Style> \r\n " +
" <Style ss:ID=\ "StringLiteral\ "> \r\n <NumberFormat " +
" ss:Format=\ "@\ "/> \r\n </Style> \r\n <Style " +
"ss:ID=\ "Decimal\ "> \r\n <NumberFormat " +
"ss:Format=\ "0.0000\ "/> \r\n </Style> \r\n " +
" <Style ss:ID=\ "Integer\ "> \r\n <NumberFormat " +
"ss:Format=\ "0\ "/> \r\n </Style> \r\n <Style " +
"ss:ID=\ "DateLiteral\ "> \r\n <NumberFormat " +
"ss:Format=\ "mm/dd/yyyy;@\ "/> \r\n </Style> \r\n " +
" </Styles> \r\n ";
const string endExcelXML = " </Workbook> ";

int rowCount = 0;
int sheetCount = 1;

excelDoc.Write(startExcelXML);
excelDoc.Write( " <Worksheet ss:Name=\ "Sheet " + sheetCount + "\ "> ");
excelDoc.Write( " <Table> ");
excelDoc.Write( " <Row> ");

//写标题
for (int i = 0; i < dataGridview1.ColumnCount; i++)
{
if (!dataGridview1.Columns[i].Visible) continue;
excelDoc.Write( " <Cell ss:StyleID=\ "BoldColumn\ "> <Data ss:Type=\ "String\ "> ");
excelDoc.Write(dataGridview1.Columns[i].HeaderText);
excelDoc.Write( " </Data> </Cell> ");
}

excelDoc.Write( " </Row> ");
for (int j = 0; j < dataGridview1.Rows.Count; j++)
{
rowCount++;
if (rowCount == 64000)