[英]save to existing excel file without opening new excel file
I have sample code block and you can see below and this code working effectively.我有示例代码块,您可以在下面看到该代码有效。 But this code not enough for me.
但是这段代码对我来说还不够。 So i need improve my code.
所以我需要改进我的代码。 Firstly, i have datagridview and this datagridview creating my datablock but i could not write new row to under before row.
首先,我有 datagridview 和这个 datagridview 创建我的数据块,但我无法在前一行下写入新行。 When i add new data block to datagridvew then new data has must write under older data on excel file.
当我将新数据块添加到 datagridvew 时,新数据必须写入 excel 文件的旧数据下。
private void buttonExcel_Click(object sender, EventArgs e)
{
// creating Excel Application
Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
// creating new WorkBook within Excel application
Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
// creating new Excelsheet in workbook
Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
// see the excel sheet behind the program
app.Visible = true;
// get the reference of first sheet. By default its name is Sheet1.
// store its reference to worksheet
worksheet = workbook.Sheets["Sheet1"];
worksheet = workbook.ActiveSheet;
// changing the name of active sheet
worksheet.Name = "Exported from gridview";
// storing header part in Excel
for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
{
worksheet.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;
}
// storing Each row and column value to excel sheet
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
worksheet.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
}
}
// save the application
workbook.SaveAs("c:\\PROJE TEKLİF FİYAT.xlsx", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
// Exit from the application
app.Quit();
}
I'd used the ClosedXML Library (via NuGet) for this as it makes saving a datatable to excel pretty simple.为此,我使用了 ClosedXML 库(通过 NuGet),因为它使将数据表保存到 excel 变得非常简单。
First, import your existing spreadsheet as a datatable like so:首先,将现有的电子表格导入为数据表,如下所示:
public DataTable GetData(string filename)
{
using (XLWorkbook workBook = new XLWorkbook(filename))
{
//Read the first Sheet from Excel file.
IXLWorksheet workSheet = workBook.Worksheet(1);
//Create a new DataTable.
DataTable dt = new DataTable();
//Loop through the Worksheet rows.
bool firstRow = true;
foreach (IXLRow row in workSheet.Rows())
{
//Use the first row to add columns to DataTable.
if (firstRow)
{
foreach (IXLCell cell in row.Cells())
{
dt.Columns.Add(cell.Value.ToString());
}
firstRow = false;
}
else
{
//Add rows to DataTable.
dt.Rows.Add();
int i = 0;
foreach (IXLCell cell in row.Cells(row.FirstCellUsed().Address.ColumnNumber, row.LastCellUsed().Address.ColumnNumber))
{
dt.Rows[dt.Rows.Count - 1][i] = cell.Value.ToString();
i++;
}
}
}
return dt;
}
}
Then convert datagridview to datatable, then merge the existing data w/ the datagridview data into one datatable, then save as XLSX.然后将datagridview转换为datatable,然后将现有数据与datagridview数据合并为一个数据表,然后保存为XLSX。
using ClosedXML.Excel;
...
private void saveDGV(DataTable existing)
{
//Creating DataTable.
DataTable dt = new DataTable();
//Adding the Columns.
foreach (DataGridViewColumn column in dataGridView1.Columns)
{
dt.Columns.Add(column.HeaderText, column.ValueType);
}
//Adding the Rows.
foreach (DataGridViewRow row in dataGridView1.Rows)
{
dt.Rows.Add();
foreach (DataGridViewCell cell in row.Cells)
{
dt.Rows[dt.Rows.Count - 1][cell.ColumnIndex] = cell.Value.ToString();
}
}
existing.Merge(dt);
using (XLWorkbook wb = new XLWorkbook())
{
wb.Worksheets.Add(existing, "Exported from gridview");
//Adjust widths of Columns.
wb.Worksheet(1).Columns().AdjustToContents();
wb.SaveAs("c:\\PROJE TEKLİF FİYAT.xlsx")
}
}
You would use this like so:你会这样使用它:
using ClosedXML.Excel;
...
private void buttonExcel_Click(object sender, EventArgs e)
{
DataTable existing = GetData("c:\\PROJE TEKLİF FİYAT.xlsx");
saveDGV(existing);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.