以下代碼中最關鍵的代碼是
Worksheet mSheet = (Microsoft.Office.Interop.Excel.Worksheet)mBook.Worksheets.Add(miss, miss, 1, Microsoft.Office.Interop.Excel.XlSheetType.xlWorksheet);
網上很資料中都沒有提到,我也是試了很久才搞出來的,上面這一行是新建了2個Sheet,想建多個就修改Add的第3個參數,數量為參數值+1
xlapp.Visible = false; //是否顯示導出過程 ,建議關閉,否則在導出過程中鼠標點擊Excel文件時會出錯。
引用:COM --》 Microsoft Office xx.x Object Library 再引用 Microsoft.Office.Interop.Excel (可以到網上搜索或以下地址下載:http://files.cnblogs.com/fan0136/Microsoft.Office.Interop.Excel.rar)
完整的代碼台下,實際使用中數據表參數可以修改成List<object>:
public class ToExcel
{
/// <summary>
/// 可以自定義導出Excel的格式,傳的參數為GridView
/// </summary>
/// <param name="gridView"></param>
/// <param name="filename"></param>
public static void ExportGridViewToExcel(DevExpress.XtraGrid.Views.Grid.GridView gridView, DevExpress.XtraGrid.Views.Grid.GridView gridView1, string filename)
{
//System.Data.DataTable dt = (System.Data.DataTable)gridView.DataSource;
SaveFileDialog sfd = new SaveFileDialog();
filename += DateTime.Now.ToString("yyyyMMdd") + "-" + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString();
sfd.FileName = filename;
sfd.Filter = "Excel files (*xls) | *.xls";
sfd.RestoreDirectory = true;
if (sfd.ShowDialog() == DialogResult.OK && sfd.FileName.Trim() != null)
{
int rowIndex = 1;
int rowIndex1 = 1;
int colIndex = 0;
int colNum = gridView.Columns.Count;
System.Reflection.Missing miss = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Excel.Application xlapp = new Microsoft.Office.Interop.Excel.Application();
xlapp.Visible = true;
Microsoft.Office.Interop.Excel.Workbooks mBooks = (Microsoft.Office.Interop.Excel.Workbooks)xlapp.Workbooks;
Microsoft.Office.Interop.Excel.Workbook mBook = (Microsoft.Office.Interop.Excel.Workbook)mBooks.Add(miss);
Worksheet mSheet = (Microsoft.Office.Interop.Excel.Worksheet)mBook.Worksheets.Add(miss, miss, 1, Microsoft.Office.Interop.Excel.XlSheetType.xlWorksheet);
Worksheet sm = mBook.Worksheets[1];
sm.Name = "q";
Worksheet ws = mBook.Worksheets[2];
ws.Name = "B";
//設置對齊方式
mSheet.Cells.HorizontalAlignment = XlHAlign.xlHAlignCenter;
//設置文字自動換行
//mSheet.Cells.WrapText = true;
//設置第一行高度,即標題欄
((Microsoft.Office.Interop.Excel.Range)mSheet.Rows["1:1", System.Type.Missing]).RowHeight = 20;
//設置數據行行高度
((Microsoft.Office.Interop.Excel.Range)mSheet.Rows["2:" + gridView.RowCount + 1, System.Type.Missing]).RowHeight = 16;
//設置字體大小(10號字體)
mSheet.Range[mSheet.Cells[1, 1], mSheet.Cells[gridView.RowCount + 1, gridView.Columns.Count]].Font.Size = 10;
//設置單元格邊框
Microsoft.Office.Interop.Excel.Range range1 = mSheet.Range[mSheet.Cells[1, 1], mSheet.Cells[gridView.RowCount + 1, gridView.Columns.Count]];
range1.Borders.LineStyle = 1;
//寫標題
for (int row = 1; row <= gridView.Columns.Count; row++)
{
sm.Cells[1, row] = gridView.Columns[row - 1].GetTextCaption();
} //寫標題
for (int row = 1; row <= gridView1.Columns.Count; row++)
{
ws.Cells[1, row] = gridView1.Columns[row - 1].GetTextCaption();
}
try
{
for (int i = 0; i < gridView.RowCount; i++)
{
rowIndex++;
colIndex = 0;
for (int j = 0; j < gridView.Columns.Count; j++)
{
colIndex++;
sm.Cells[rowIndex, colIndex] = gridView.GetRowCellValue(i, gridView.Columns[j]);
}
}
for (int i = 0; i < gridView1.RowCount; i++)
{
rowIndex1++;
colIndex = 0;
for (int j = 0; j < gridView1.Columns.Count; j++)
{
colIndex++;
ws.Cells[rowIndex1, colIndex] = gridView1.GetRowCellValue(i, gridView1.Columns[j]);
}
}
mBook.SaveAs(sfd.FileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel7, miss, miss, miss, miss, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
miss, miss, miss, miss, miss);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
mBooks.Close();
xlapp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(mSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(mBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(mBooks);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlapp);
GC.Collect();
}
}
else
{
//return false;
}
}
}
