c# 导出Excel 怎么设置格式!
导出代码为!!
public bool SaveToExcel(System.Data.DataTable table, string fileName)
{
if (table.Rows.Count == 0)
{
return false;
}
Microsoft.Office.Interop.Excel.Application excel = new ApplicationClass();
int rowindex = 1;
int colindex = 0;
Workbook work = excel.Workbooks.Add(true); ;
foreach (DataColumn col in table.Columns)
{
colindex++;
excel.Cells[1, colindex] = col.ColumnName;
}
foreach (DataRow row in table.Rows)
{
rowindex++;
colindex = 0;
foreach (DataColumn col in table.Columns)
{
colindex++;
excel.Cells[rowindex, colindex] = row[col.ColumnName].ToString();
}
}
excel.Visible = false;
excel.ActiveWorkbook.SaveAs(fileName, XlFileFormat.xlExcel8, null, null, false, false, XlSaveAsAccessMode.xlNoChange, null, null, null, null, null);
excel.Quit();
excel = null;
GC.Collect();
MessageBox.Show("已保存!");
return true;
}
求助怎么设置格式
[解决办法]
refer:http://www.cnblogs.com/cwy173/archive/2012/02/28/2371199.html
[解决办法]
//文本格式 ((Microsoft.Office.Interop.Excel.Range)worksheet.Columns[i]).NumberFormatLocal = "@";
//数字格式 ((Microsoft.Office.Interop.Excel.Range)worksheet.Columns[i]).NumberFormatLocal = "0";