Reputation: 35
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. When i add new data block to datagridvew then new data has must write under older data on excel file.
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();
}
Upvotes: 2
Views: 880
Reputation: 982
I'd used the ClosedXML Library (via NuGet) for this as it makes saving a datatable to excel pretty simple.
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.
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);
}
Upvotes: 1