读书人

c# 导出Excel 如何设置格式

发布时间: 2013-09-29 11:07:08 作者: rapoo

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";

这是个例子,还有很多格式,网上一搜索就有

读书人网 >C#

热点推荐