将数据从 datagridview 保存到 excel 文件
Save data from datagridview to excel file
我已经创建了将数据从 datagridview 保存到 excel 文件的函数。
保存函数:
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 < dataGridView2.Columns.Count; i++)
{
worksheet.Cells[1, i + 1] = dataGridView2.Columns[i].HeaderText;
}
for (int i = 0; i < dataGridView2.Rows.Count; i++)
{
for (int j = 0; j < dataGridView2.Columns.Count; j++)
{
if (dataGridView2.Rows[i].Cells[j].Value != null)
{
worksheet.Cells[i + 2, j + 1] = dataGridView2.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()); }
它在我的电脑上运行良好,版本为:
我尝试 运行 在我安装的第二台计算机上执行此操作 :
我需要更改代码以适用于两个版本吗?
我正在寻找可以执行相同但适用于每个办公室安装的示例。
顺便说一句,当我尝试在第二台计算机上 运行 时出现此错误:
如果您不一定要使用 MS Office 组件,那么我建议您改用 EPPlus 库。
string saveasFileName = .....
using (var package = new ExcelPackage())
{
using (var worksheet = package.Workbook.Worksheets.Add("Records"))
{
worksheet.Cells[1, 1].Value = "Records from dataGridView2:";
worksheet.Cells[1, 1].Style.Font.Bold = true;
// column headers
for (int i = 0; i < dataGridView2.Columns.Count; i++)
{
worksheet.Cells[2, i + 1].Value = dataGridView2.Columns[i].HeaderText;
}
// actual data
for (int i = 0; i < dataGridView2.Rows.Count; i++)
{
for (int j = 0; j < dataGridView2.Columns.Count; j++)
{
// ... populate worksheet ...
worksheet.Cells[i + 3, j + 1].Value = dataGridView2.Rows[i].Cells[j].Value?.ToString()??"";
}
}
// save
package.SaveAs(saveasFileName);
}
}
我在 https://github.com/siccolo/EppPlus_CreateExcel/blob/master/Excel.cs
有一个例子
@Adamszsz - 如果您需要打开一个 excel 文件并加载到 gridview 中,那么您可以使用 oledb 连接,例如: - 打开 excel 文件 .xls :
...
var excelconnection = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + filePath + ";Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\"";
var table = "[Sheet1$]"
// using System.Data.OleDb
var excel = new OleDbDataAdapter("SELECT * FROM " + table, excelconnection);
var exceldata = new DataTable();
excel.Fill(exceldata);
...
而对于 excel 文件 .xlsx 使用 Net.SourceForge.Koogra.Excel2007.
这并不能直接回答您的代码抛出该错误的原因。但作为一种对我有用的替代方法,是否值得尝试?我使用了来自 Nuget 的 'ClosedXML' 包...可能还有其他选项,比如 'yob's reply,我相信它也可以正常工作。
using ClosedXML.Excel;
然后保存数据:
SaveFileDialog saveFile1 = new SaveFileDialog();
saveFile1.Filter = "Excel file|*.xlsx";
saveFile1.Title = "save results as Excel spreadsheet";
saveFile1.FileName = title + " -" + DateTime.Now.ToString("yyyyMMdd") + ".xlsx";
if (saveFile1.ShowDialog() == DialogResult.OK)
{
var wb = new XLWorkbook();
var ws = wb.Worksheets.Add(data, title);
wb.SaveAs(saveFile1.FileName);
}
'data'是数据表,所以你需要先将datagridview转换成数据表。正如我所说,这不是对您现有代码的回答,而是对我有用的可能替代方案:)祝你好运。
我已经创建了将数据从 datagridview 保存到 excel 文件的函数。
保存函数:
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 < dataGridView2.Columns.Count; i++)
{
worksheet.Cells[1, i + 1] = dataGridView2.Columns[i].HeaderText;
}
for (int i = 0; i < dataGridView2.Rows.Count; i++)
{
for (int j = 0; j < dataGridView2.Columns.Count; j++)
{
if (dataGridView2.Rows[i].Cells[j].Value != null)
{
worksheet.Cells[i + 2, j + 1] = dataGridView2.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()); }
它在我的电脑上运行良好,版本为:
我尝试 运行 在我安装的第二台计算机上执行此操作 :
我需要更改代码以适用于两个版本吗?
我正在寻找可以执行相同但适用于每个办公室安装的示例。
顺便说一句,当我尝试在第二台计算机上 运行 时出现此错误:
如果您不一定要使用 MS Office 组件,那么我建议您改用 EPPlus 库。
string saveasFileName = .....
using (var package = new ExcelPackage())
{
using (var worksheet = package.Workbook.Worksheets.Add("Records"))
{
worksheet.Cells[1, 1].Value = "Records from dataGridView2:";
worksheet.Cells[1, 1].Style.Font.Bold = true;
// column headers
for (int i = 0; i < dataGridView2.Columns.Count; i++)
{
worksheet.Cells[2, i + 1].Value = dataGridView2.Columns[i].HeaderText;
}
// actual data
for (int i = 0; i < dataGridView2.Rows.Count; i++)
{
for (int j = 0; j < dataGridView2.Columns.Count; j++)
{
// ... populate worksheet ...
worksheet.Cells[i + 3, j + 1].Value = dataGridView2.Rows[i].Cells[j].Value?.ToString()??"";
}
}
// save
package.SaveAs(saveasFileName);
}
}
我在 https://github.com/siccolo/EppPlus_CreateExcel/blob/master/Excel.cs
有一个例子@Adamszsz - 如果您需要打开一个 excel 文件并加载到 gridview 中,那么您可以使用 oledb 连接,例如: - 打开 excel 文件 .xls :
...
var excelconnection = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + filePath + ";Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\"";
var table = "[Sheet1$]"
// using System.Data.OleDb
var excel = new OleDbDataAdapter("SELECT * FROM " + table, excelconnection);
var exceldata = new DataTable();
excel.Fill(exceldata);
...
而对于 excel 文件 .xlsx 使用 Net.SourceForge.Koogra.Excel2007.
这并不能直接回答您的代码抛出该错误的原因。但作为一种对我有用的替代方法,是否值得尝试?我使用了来自 Nuget 的 'ClosedXML' 包...可能还有其他选项,比如 'yob's reply,我相信它也可以正常工作。
using ClosedXML.Excel;
然后保存数据:
SaveFileDialog saveFile1 = new SaveFileDialog();
saveFile1.Filter = "Excel file|*.xlsx";
saveFile1.Title = "save results as Excel spreadsheet";
saveFile1.FileName = title + " -" + DateTime.Now.ToString("yyyyMMdd") + ".xlsx";
if (saveFile1.ShowDialog() == DialogResult.OK)
{
var wb = new XLWorkbook();
var ws = wb.Worksheets.Add(data, title);
wb.SaveAs(saveFile1.FileName);
}
'data'是数据表,所以你需要先将datagridview转换成数据表。正如我所说,这不是对您现有代码的回答,而是对我有用的可能替代方案:)祝你好运。