![]() |
#2
Maick2015-06-13 09:56
public static void DoForExcel(DataGridView dgv, string reportTitle)
{ #region 验证可操作性 //定义表格内数据的行数和列数 int rowscount = dgv.Rows.Count; int colscount = dgv.Columns.Count; //行数必须大于0 if (rowscount <= 0) { MessageBox.Show("没有数据可供保存", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } //列数必须大于0 if (colscount <= 0) { MessageBox.Show("没有数据可供保存", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } //行数不可以大于65536 if (rowscount > 65536) { MessageBox.Show("数据记录数太多(最多不能超过65536条),不能保存", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } //列数不可以大于255 if (colscount > 255) { MessageBox.Show("数据记录行数太多,不能保存", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } #endregion 验证可操作性 Excel.Application xlApp = new Excel.ApplicationClass(); if (xlApp == null) { MessageBox.Show("Excel无法启动", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } int rowIndex = 2; int colIndex = 0; Excel.Workbook xlBook = xlApp.Workbooks.Add(true); Excel.Range range = xlApp.get_Range(xlApp.Cells[1, 1], xlApp.Cells[1, dgv.ColumnCount]); range.MergeCells = true; xlApp.ActiveCell.FormulaR1C1 = reportTitle; xlApp.ActiveCell.Font.Size = 14; xlApp.ActiveCell.Font.Bold = true; foreach (DataGridViewColumn column in dgv.Columns) { colIndex = colIndex + 1; xlApp.Cells[2, colIndex] = column.HeaderText; } for (int row = 0; row < dgv.Rows.Count; row++) { rowIndex = rowIndex + 1; for (int col = 0; col < dgv.Columns.Count; col++) { if (dgv.Rows[row].Cells[col].ValueType == System.Type.GetType("System.String")) { xlApp.Cells[rowIndex, col + 1] = "'" + dgv.Rows[row].Cells[col].Value.ToString(); } else { xlApp.Cells[rowIndex, col + 1] = dgv.Rows[row].Cells[col].Value.ToString(); } } } xlApp.get_Range(xlApp.Cells[2, 1], xlApp.Cells[2, dgv.Columns.Count]).Font.Bold = true; xlApp.get_Range(xlApp.Cells[2, 1], xlApp.Cells[rowIndex, colIndex]).Borders.LineStyle = 1; xlApp.Cells.EntireColumn.AutoFit(); xlApp.Cells.VerticalAlignment = Excel.Constants.xlCenter; xlApp.Cells.HorizontalAlignment = Excel.Constants.xlLeft; try { xlApp.Save(System.DateTime.Now.Millisecond.ToString()); } catch { } finally { xlApp.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBook); System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp); GC.Collect(); } } |
//创建Excel对象
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
excel.Application.Workbooks.Add(true);
//生成字段名称
for (int i = 0; i < dataGridView5.ColumnCount; i++)
{
excel.Cells[1, i + 1] = dataGridView5.Columns[i].HeaderText;
}
//填充数据
for (int i = 0; i < dataGridView5.RowCount - 1; i++) //循环行
{
for (int j = 0; j < dataGridView5.ColumnCount; j++) //循环列
{
if (dataGridView5[j, i].ValueType == typeof(string))
{
excel.Cells[i + 2, j + 1] = "'" + dataGridView5.Rows[i].Cells[j].Value.ToString();
}
else
{
excel.Cells[i + 2, j + 1] = dataGridView5;
}
}
//设置禁止弹出保存和覆盖的询问提示框
excel.Visible = false;
excel.DisplayAlerts = false;
excel.AlertBeforeOverwriting = false;
//保存到临时工作簿
//excel.Application.Workbooks.Add(true).Save();
//保存文件
excel.Save("D:" + "\\234.xls");
excel.Quit();
}
每次到这一句 excel.Application.Workbooks.Add(true);的时候,就弹错
无法将类型为“Microsoft.Office.Interop.Excel.ApplicationClass”的 COM 对象强制转换为接口类型“Microsoft.Office.Interop.Excel._Application”。此操作失败的原因是对 IID 为“{000208D5-0000-0000-C000-000000000046}”的接口的 COM 组件调用 QueryInterface 因以下错误而失败: 加载类型库/DLL 时出错。 (异常来自 HRESULT:0x80029C4A (TYPE_E_CANTLOADLIBRARY))。
哪位大神指点一下,感激不尽
[ 本帖最后由 xintuhai 于 2015-6-12 22:08 编辑 ]