先贴一段最最简单的代码,直接生成Excel
public bool ExportDataGridView(DataGridView dgv, bool isShowExcel)
{
if (dgv.Rows.Count == 0)
{
return false;
}
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
excel.Application.Workbooks.Add(true);
excel.Visible = isShowExcel;
for (int i = 0; i < dgv.ColumnCount; i++)
{
excel.Cells[1, i + 1] = dgv.Columns[i].HeaderText;
}
for (int i = 0; i<dgv.RowCount - 1; i++)
{
for (int j = 0; j < dgv.ColumnCount; j++)
{
if (dgv[j, i].ValueType == typeof(string))
{
excel.Cells[i + 2, j + 1] = "" + dgv[j, i].Value.ToString();
}
else
{
excel.Cells[i + 2, j + 1] = dgv[j, i].Value.ToString();
}
}
}
return true;
}
具体的需要弹出对话框,设置格式的:
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
public void print(System.Windows.Forms.DataGridView dataGridView1)
{
//实例化一个Excel.Application对象
Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
//导出到execl
try
{
//让后台执行设置为不可见,为true的话会看到打开一个Excel,然后数据在往里写
app.Visible = false;
//没有数据的话就不往下执行
if (dataGridView1.Rows.Count == 0)
return;
//新增加一个工作簿,Workbook是直接保存,不会弹出保存对话框,加上Application会弹出保存对话框,值为false会报错
Microsoft.Office.Interop.Excel.Workbooks workbooks = app.Workbooks;
Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(true);
//新建wSheet
Microsoft.Office.Interop.Excel.Worksheet wSheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
wSheet.Name ="报警数据";
//生成Excel中列头名称
for (int i = 6; i < dataGridView1.Columns.Count-1; i++)
{
wSheet.Cells[1, i - 5] = dataGridView1.Columns[i].HeaderText;
wSheet.get_Range(wSheet.Cells[1, i - 5], wSheet.Cells[1, i - 5]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
wSheet.get_Range(wSheet.Cells[1, i - 5], wSheet.Cells[1, i - 5]).Font.Bold = true;
//设置行高
wSheet.get_Range(wSheet.Cells[1, i - 5], wSheet.Cells[1, i - 5]).RowHeight = 20;
}
//把DataGridView当前页的数据保存在Excel中
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
for (int j = 6; j < dataGridView1.Columns.Count-1; j++)
{
wSheet.Cells[i + 2, j - 5] = "'" + dataGridView1[j, i].Value.ToString();
//设置行高
wSheet.get_Range(wSheet.Cells[i + 2, j - 5], wSheet.Cells[i + 2, j - 5]).RowHeight = 20;
//画表格线
wSheet.get_Range(wSheet.Cells[i + 2, j - 5], wSheet.Cells[i + 2, j - 5]).BorderAround(
Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin,
Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, Type.Missing);
}
}
app.DisplayAlerts = false;
app.AlertBeforeOverwriting = false;
//文件的名称
string FileName = app.GetSaveAsFilename(DateTime.Now.ToShortDateString()+"报警数据"
+ ".xls", "Excel文件 (*.xls), *.xls", 1, "报警数据", "报警数据").ToString();
//保存Excel文件
workbook.SaveAs(FileName, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
//关闭workbook
workbook.Close(Type.Missing, Type.Missing, Type.Missing);
workbooks.Close();
app.Quit();
//删除Excel进程
IntPtr t = new IntPtr(app.Hwnd);
int k = 0;
GetWindowThreadProcessId(t, out k);
System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
p.Kill();
app = null;
}
catch (Exception ex)
{
IntPtr t = new IntPtr(app.Hwnd);
int k = 0;
GetWindowThreadProcessId(t, out k);
System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
p.Kill();
app = null;
MessageBox.Show(ex.Message, "错误提示");
}
}
#endregion
浙公网安备 33010602011771号