Tengo un error al exportar un Excel cuando llamo a una función FOR este es mi script:
Lo estoy trabajando con un la librería MICROSOFT.EXCEL, me sale el error cuando exporto en casos múltiples
private void ExportDataSetToExcel(DataSet ds, string strPath)
{
int inHeaderLength = 3, inColumn = 0, inRow = 0;
System.Reflection.Missing Default = System.Reflection.Missing.Value;
//Create Excel File
strPath += @"\Excel" + "hoja" + ".xlsx";
OfficeExcel.Application excelApp = new OfficeExcel.Application();
OfficeExcel.Workbook excelWorkBook = excelApp.Workbooks.Add(1);
foreach (DataTable dtbl in ds.Tables)
{
//Create Excel WorkSheet
OfficeExcel.Worksheet excelWorkSheet = excelWorkBook.Sheets.Add(Default, excelWorkBook.Sheets[excelWorkBook.Sheets.Count], 1, Default);
excelWorkSheet.Name = dtbl.TableName;//Name worksheet
//excelWorkSheet.UsedRange.Copy(Type.Missing);
//Write Column Name
for (int i = 0; i < dtbl.Columns.Count; i++)
excelWorkSheet.Cells[inHeaderLength + 1, i + 1] = dtbl.Columns[i].ColumnName.ToUpper();
//Write Rows
for (int m = 0; m < dtbl.Rows.Count; m++)
{
for (int n = 0; n < dtbl.Columns.Count; n++)
{
inColumn = n + 1;
inRow = inHeaderLength + 2 + m;
excelWorkSheet.Cells[inRow, inColumn] = dtbl.Rows[m].ItemArray[n].ToString();
if (m % 2 == 0)
{
excelWorkSheet.get_Range("A" + inRow.ToString(), "K" + inRow.ToString()).Interior.Color = System.Drawing.ColorTranslator.FromHtml("#DFF0F3");
excelWorkSheet.get_Range("L" + inRow.ToString(), "M" + inRow.ToString()).Interior.Color = System.Drawing.ColorTranslator.FromHtml("#808B96");
excelWorkSheet.get_Range("L" + inRow.ToString(), "M" + inRow.ToString()).Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.White);
excelWorkSheet.get_Range("A" + inRow.ToString(), "M" + inRow.ToString()).Borders.LineStyle= OfficeExcel.XlLineStyle.xlContinuous;
excelWorkSheet.get_Range("A" + inRow.ToString(), "M" + inRow.ToString()).Borders.Weight= OfficeExcel.XlBorderWeight.xlThin;
}
else
excelWorkSheet.get_Range("L" + inRow.ToString(), "M" + inRow.ToString()).Interior.Color = System.Drawing.ColorTranslator.FromHtml("#808B96");
excelWorkSheet.get_Range("L" + inRow.ToString(), "M" + inRow.ToString()).Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.White);
excelWorkSheet.get_Range("A" + inRow.ToString(), "M" + inRow.ToString()).Borders.LineStyle = OfficeExcel.XlLineStyle.xlContinuous;
excelWorkSheet.get_Range("A" + inRow.ToString(), "M" + inRow.ToString()).Borders.Weight = OfficeExcel.XlBorderWeight.xlThin;
}
}
OfficeExcel.Range cellRang = excelWorkSheet.get_Range("A1", "M3");
cellRang.Merge(false);
cellRang.Interior.Color = System.Drawing.Color.White;
cellRang.Font.Color = System.Drawing.Color.Gray;
cellRang.HorizontalAlignment = OfficeExcel.XlHAlign.xlHAlignCenter;
cellRang.VerticalAlignment = OfficeExcel.XlVAlign.xlVAlignCenter;
cellRang.Font.Size = 26;
excelWorkSheet.Cells[1, 1] = "Detalle Estado Cuenta " + lst[0].Razon_Social;
//Style table column names
cellRang = excelWorkSheet.get_Range("A4", "K4");
cellRang.Borders.LineStyle = OfficeExcel.XlLineStyle.xlContinuous;
cellRang.Borders.Weight = OfficeExcel.XlBorderWeight.xlThin;
cellRang.Font.Bold = true;
cellRang.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.White);
cellRang.Interior.Color = System.Drawing.ColorTranslator.FromHtml("#1A5276");
cellRang = excelWorkSheet.get_Range("L4", "M4");
cellRang.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.White);
cellRang.Interior.Color = System.Drawing.ColorTranslator.FromHtml("#808B96");
excelWorkSheet.get_Range("A4", "M4").EntireColumn.HorizontalAlignment = OfficeExcel.XlHAlign.xlHAlignCenter;
cellRang.Borders.LineStyle = OfficeExcel.XlLineStyle.xlContinuous;
cellRang.Borders.Weight = OfficeExcel.XlBorderWeight.xlThin;
//cellRang.PrintPreview();
// excelWorkSheet.get_Range("F4").EntireColumn.HorizontalAlignment = OfficeExcel.XlHAlign.xlHAlignRight;
//Formate price column
// excelWorkSheet.get_Range("F5").EntireColumn.NumberFormat = "0.00";
//Auto fit columns
excelWorkSheet.Columns.AutoFit();
}
//Delete First Page
excelApp.DisplayAlerts = false;
Microsoft.Office.Interop.Excel.Worksheet lastWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)excelWorkBook.Worksheets[1];
lastWorkSheet.Delete();
// excelApp.DisplayAlerts = true;
//Set Defualt Page
(excelWorkBook.Sheets[1] as OfficeExcel._Worksheet).Activate();
excelWorkBook.SaveAs(strPath, Default, Default, Default, false, Default, OfficeExcel.XlSaveAsAccessMode.xlNoChange, Default, Default, Default, Default, Default);
excelWorkBook.Close();
excelApp.Quit();
// MessageBox.Show("Excel generated successfully \n As " + strPath);
}
Este es mi error: