ahojte, pracujem na jednom programe, ktorý každú hodinu načíta dáta a exportuje ich do excelu, samozrejme dokument uloží
všetko funguje ako má keď to robí prvý krát, no ako sa spustí export druhý krát, tak pri ukladaní skončí chybou incorrect function, už naozaj neviem čo mám robiť :(
private void ExportExcel()
{
lblStatus.Text = "Exportujem...";
string excelName = DateTime.Now.ToShortDateString() + "_" + DateTime.Now.ToShortTimeString();
excelName = excelName.Replace(".", "-");
excelName = "\\"+ excelName.Replace(":", "-");
System.Globalization.CultureInfo before = System.Threading.Thread.CurrentThread.CurrentCulture;
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
int xRows = Grid1.RowCount;
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
//xlApp.Visible = true;
Workbook wb = xlApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
Worksheet ws = (Worksheet)wb.Worksheets[1];
//hlavicka
ws.Cells[1, 1] = "Workcenter";
ws.Cells[1, 2] = "Operation";
ws.Cells[1, 3] = "Batch";
ws.Cells[1, 4] ="Part No";
ws.Cells[1, 5] ="Popis";
ws.Cells[1, 6] = "Balenie";
ws.Cells[1, 7] = "Stav";
ws.Cells[1, 8] = "Zaciatok";
ws.Cells[1, 9] = "Koniec";
ws.Cells[1, 10] = "Demand";
ws.Cells[1, 11] = "Vyrobene";
ws.Cells[1, 12] = "Posl zmena";
ws.Cells[1, 13] = "Mach Run Factor";
ws.Cells[1, 14] = "Ostavajuci cas vyroby (hod)";
//obsah gridu
for (int i = 1; i < xRows + 1; i++)
{
try
{
ws.Cells[i + 1, 1] = Grid1.Rows[i].Cells[0].Value;
ws.Cells[i + 1, 2] = Grid1.Rows[i].Cells[1].Value;
ws.Cells[i + 1, 3] = Grid1.Rows[i].Cells[2].Value;
ws.Cells[i + 1, 4] = Grid1.Rows[i].Cells[3].Value;
ws.Cells[i + 1, 5] = Grid1.Rows[i].Cells[4].Value;
ws.Cells[i + 1, 6] = Grid1.Rows[i].Cells[5].Value;
ws.Cells[i + 1, 7] = Grid1.Rows[i].Cells[6].Value;
ws.Cells[i + 1, 8] = Grid1.Rows[i].Cells[7].Value;
ws.Cells[i + 1, 9] = Grid1.Rows[i].Cells[8].Value;
ws.Cells[i + 1, 10] = Grid1.Rows[i].Cells[9].Value;
ws.Cells[i + 1, 11] = Grid1.Rows[i].Cells[10].Value;
ws.Cells[i + 1, 12] = Grid1.Rows[i].Cells[11].Value;
ws.Cells[i + 1, 13] = Grid1.Rows[i].Cells[12].Value;
ws.Cells[i + 1, 14] = Grid1.Rows[i].Cells[13].Value;
}
catch
{
lblStatus.Text = "Nastala chyba pri exporte";
}
}
try
{
lblStatus.Text = "Ukladam...";
//ukladanie dokumentu
//wb.SaveAs(strSavePath + excelName, XlFileFormat.xlOpenXMLWorkbook, System.Reflection.Missing.Value, System.Reflection.Missing.Value, false, false, XlSaveAsAccessMode.xlNoChange, XlSaveConflictResolution.xlUserResolution, true, System.Reflection.Missing.Value, System.Reflection.Missing.Value,System.Reflection.Missing.Value);
wb.SaveAs(strSavePath + excelName);
lblStatus.Text = "Ulozene...";
wb.Close(false, System.Reflection.Missing.Value, System.Reflection.Missing.Value);
wb = null;
ws = null;
xlApp = null;
}
catch
{
lblStatus.Text = "Nastala chyba pri ukladani";
}
System.Threading.Thread.CurrentThread.CurrentUICulture = before;
}