将Datagridview导出到Excel中的Excel#
问题描述:
此代码在导出excel后总是跳过最后一行,你能检查代码中的错误吗?将Datagridview导出到Excel中的Excel#
我改变
transcationTableDataGridView.Rows.Count - 1
到
transcationTableDataGridView.Rows.Count + 1
它导出所有行到excel,但抛出指数应该是非负的错误此异常:
private void exportToExcel_Click(object sender, EventArgs e)
{
try
{
Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
app.Visible = true;
worksheet = workbook.Sheets["Sheet1"];
worksheet = workbook.ActiveSheet;
worksheet.Name = "Records";
try
{
for (int i = 1; i < transcationTableDataGridView.Columns.Count + 1; i++)
{
worksheet.Cells[1, i] = transcationTableDataGridView.Columns[i - 1].HeaderText;
}
for (int i = 0; i < transcationTableDataGridView.Rows.Count - 1; i++)
{
for (int j = 0; j < transcationTableDataGridView.Columns.Count; j++)
{
if (transcationTableDataGridView.Rows[i].Cells[j].Value != null)
{
worksheet.Cells[i + 2, j + 1] = transcationTableDataGridView.Rows[i].Cells[j].Value.ToString();
}
else
{
worksheet.Cells[i + 2, j + 1] = "";
}
}
}
//Getting the location and file name of the excel to save from user.
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.Filter = "Excel files (*.xlsx)|*.xlsx|All files (*.*)|*.*";
saveDialog.FilterIndex = 2;
if (saveDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
workbook.SaveAs(saveDialog.FileName);
MessageBox.Show("Export Successful", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
app.Quit();
workbook = null;
worksheet = null;
}
}
catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); }
}
这也给了我一些电脑
我使用这个代码导出过程中的错误:这个代码
using Excel=Microsoft.Office.Interop.Excel;
:
using System.Runtime.InteropServices;
谁能解释什么是两者之间的区别?
答
private void exportToExcel_Click(object sender, EventArgs e)
{
try
{
Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
app.Visible = true;
worksheet = workbook.Sheets["Sheet1"];
worksheet = workbook.ActiveSheet;
worksheet.Name = "Records";
try
{
for (int i = 0; i < transcationTableDataGridView.Columns.Count; i++)
{
worksheet.Cells[1, i + 1] = transcationTableDataGridView.Columns[i].HeaderText;
}
for (int i = 0; i < transcationTableDataGridView.Rows.Count; i++)
{
for (int j = 0; j < transcationTableDataGridView.Columns.Count; j++)
{
if (transcationTableDataGridView.Rows[i].Cells[j].Value != null)
{
worksheet.Cells[i + 2, j + 1] = transcationTableDataGridView.Rows[i].Cells[j].Value.ToString();
}
else
{
worksheet.Cells[i + 2, j + 1] = "";
}
}
}
//Getting the location and file name of the excel to save from user.
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.Filter = "Excel files (*.xlsx)|*.xlsx|All files (*.*)|*.*";
saveDialog.FilterIndex = 2;
if (saveDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
workbook.SaveAs(saveDialog.FileName);
MessageBox.Show("Export Successful", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
app.Quit();
workbook = null;
worksheet = null;
}
}
catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); }
}
+0
谢谢,它的工作原理,你能提供答案我的底部问题? – Patrick
+0
当您在Used PC中使用Microsoft.Office.Interop.Excel库时,必须安装Microsoft Excel程序。也许没有Excel程序目前比较 –
当您在Used PC中使用Microsoft.Office.Interop.Excel库时必须具有Microsoft Excel程序。也许没有Excel程序当前比较 –