EDN Admin
Well-known member
<pre class="prettyprint Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelSheets.get_Item(currentSheet);
xlNewsheet = (Excel.Worksheet)wb.Worksheets.Add();
for (int k = 0; k < this.dataGridViewCon.Columns.Count; k++){
excelWorksheet.Cells[1, k + 1] = this.dataGridViewCon.Columns[k].HeaderText;
}
for (k = 0; k <= dataGridViewCon.RowCount - 1; k++)
{
for (int a = 1; a <= wb.Worksheets.Count; a++)
{
if (wb.Worksheets.Item[a].Name != "SFINR" + k.ToString())
{
xlNewsheet = (Excel.Worksheet)excel.Worksheets.Add(missing, missing, missing, missing);
sheetname = "SFINR" + k.ToString();
xlNewsheet.Name = sheetname;
}
DataGridViewRow row = this.dataGridViewCon.Rows[k];
for (int m = 0; m <= dataGridViewCon.ColumnCount - 1; m++)
{
DataGridViewCell cell = dataGridViewCon[m, k];
excelWorksheet.Cells[k + 2, m + 1] = cell.Value;
}
}
}
string tmpName = Path.GetTempFileName();
File.Delete(tmpName);
wb.SaveAs(tmpName, missing, missing, missing, missing, missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, missing, missing, missing, missing, missing);
wb.Close(false, missing, missing);
excel.Quit();
File.Delete(Fname);
File.Move(tmpName, Fname);
excelApp.Quit();[/code]
Hello.
I want to create a new sheet in excel for every new row in my datagridview. I works perfectly fine when I do it for the first time. But when i import the excel workbook to the datagridview, and creating new rows, i get error:
<span style="color:#ff0000; font-size:x-small <span style="color:#ff0000; font-size:x-small <span lang="NO-BOK
Error: System.Runtime.InteropServices.COMException (0x800A03EC): Cannot rename a sheet to the same name as another sheet, a referenced object library or a workbook referenced by Visual Basic.
View the full article
xlNewsheet = (Excel.Worksheet)wb.Worksheets.Add();
for (int k = 0; k < this.dataGridViewCon.Columns.Count; k++){
excelWorksheet.Cells[1, k + 1] = this.dataGridViewCon.Columns[k].HeaderText;
}
for (k = 0; k <= dataGridViewCon.RowCount - 1; k++)
{
for (int a = 1; a <= wb.Worksheets.Count; a++)
{
if (wb.Worksheets.Item[a].Name != "SFINR" + k.ToString())
{
xlNewsheet = (Excel.Worksheet)excel.Worksheets.Add(missing, missing, missing, missing);
sheetname = "SFINR" + k.ToString();
xlNewsheet.Name = sheetname;
}
DataGridViewRow row = this.dataGridViewCon.Rows[k];
for (int m = 0; m <= dataGridViewCon.ColumnCount - 1; m++)
{
DataGridViewCell cell = dataGridViewCon[m, k];
excelWorksheet.Cells[k + 2, m + 1] = cell.Value;
}
}
}
string tmpName = Path.GetTempFileName();
File.Delete(tmpName);
wb.SaveAs(tmpName, missing, missing, missing, missing, missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, missing, missing, missing, missing, missing);
wb.Close(false, missing, missing);
excel.Quit();
File.Delete(Fname);
File.Move(tmpName, Fname);
excelApp.Quit();[/code]
Hello.
I want to create a new sheet in excel for every new row in my datagridview. I works perfectly fine when I do it for the first time. But when i import the excel workbook to the datagridview, and creating new rows, i get error:
<span style="color:#ff0000; font-size:x-small <span style="color:#ff0000; font-size:x-small <span lang="NO-BOK
Error: System.Runtime.InteropServices.COMException (0x800A03EC): Cannot rename a sheet to the same name as another sheet, a referenced object library or a workbook referenced by Visual Basic.
View the full article