日期:2014-05-17 浏览次数:20794 次
public bool ExportExcel(DataGridViewX dtSource, string saveFileName)
{
DataTable dt = (DataTable)dtSource.DataSource;
if (dt.Rows.Count == 0
------解决方案--------------------
dt==null)
{
return false;
}
else
{
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);//创建sheet
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];//写死,只操作sheet1
//添加列标题
for (int i = 0; i < dt.Columns.Count; i++)
{
worksheet.Cells[1, i + 1] = dt.Columns[i].ColumnName;
}
//添加内容
for (int r = 0; r < dt.Rows.Count; r++)