第一种方式
public static void ExportGridViewToExcel(DevExpress.XtraGrid.Views.Grid.GridView gridView, 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 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);
Microsoft.Office.Interop.Excel.Worksheet mSheet = (Microsoft.Office.Interop.Excel.Worksheet)mBook.ActiveSheet;
Microsoft.Office.Interop.Excel.Range mRange = mSheet.get_Range((object)"A1", System.Reflection.Missing.Value);
//mSheet.Columns.
//设置对齐方式
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.get_Range(mSheet.Cells[1, 1], mSheet.Cells[gridView.RowCount + 1, gridView.Columns.Count]).Font.Size = 10;
//设置单元格边框
Microsoft.Office.Interop.Excel.Range range1 = mSheet.get_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++)
{
mSheet.Cells[1, row] = gridView.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++;
mSheet.Cells[rowIndex, colIndex] = gridView.GetRowCellValue(i, gridView.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);
//return true;
}
catch (Exception ex)
{
//throw new Exception(ex.Message);
}
finally
{
//mBook.Close(false, miss, miss);
mBooks.Close();
xlapp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(mRange);
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;
}
}
ExportGridViewToExcel((DevExpress.XtraGrid.Views.Grid.GridView)gridControl1.MainView, @"c:\\test.xls");
第二种方式
string strFileName = "清查表";
string strSavePath = "";
SaveFileDialog dlgSave = new SaveFileDialog();
dlgSave.Title = "导出到Microsoft Excel Document";// 对话框题目
dlgSave.FileName = strFileName;
dlgSave.Filter = "Microsoft Excel|*.xls";// 过滤格局
if(dlgSave.ShowDialog()== System.Windows.Forms.DialogResult.OK)
{
strSavePath = dlgSave.FileName;
}
if(strSavePath != "")
{
//this.gridControl1.MainView.
DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions(DevExpress.XtraPrinting.TextExportMode.Value);
//gridControl1.MainView.ExportToExcelOld(@"c:\\test.xls");
// options.TextExportMode=
gridControl1.ExportToXls(@"c:\\test.xls", options);
}