Bu hata bölgesel ayar uyumsuzluğundan oluşur. Aşağıdaki gibi yeni bir workbook oluşturmak istediğimizde alırız:
Microsoft.Office.Interop.Excel.Workbook wb = xlApp.Workbooks.Add(true);
Hatayı geçmek için excel işlemleri esnasında bölgesel ayarlarımızı "en-us" olarak değiştirmemiz gerekir. Tabi sonrasında geri eski haline getirmeyde unutmamak gerek.
Örnek kod:
using System.Xml.Linq;
using System.Reflection;
using Microsoft.Office.Interop.Excel;
private void ExcelOlustur(string pDosyaAdi, string pXML)
{
Logla("Excel dosyası oluşturuluyor...");
System.Globalization.CultureInfo myCultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture;
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-us");
try
{
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
if (xlApp == null)
{
Logla("Excel oluşturulamadı : " + pDosyaAdi);
return;
}
xlApp.Visible = true;
Microsoft.Office.Interop.Excel.Workbook wb = xlApp.Workbooks.Add(true);
Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Sheets[1];
XElement xe = XElement.Parse(pXML);
Logla(xe.Elements().Count().ToString());
ws.Cells[1, 1] = "PERSONEL NO";
ws.Cells[1, 2] = "AD SOYAD";
ws.Cells[1, 3] = "TARİH";
((Range)ws.Cells[1, 1]).Font.Bold = true;
((Range)ws.Cells[1, 2]).Font.Bold = true;
((Range)ws.Cells[1, 3]).Font.Bold = true;
int mRow = 2;
foreach (XElement p in xe.Descendants(XName.Get("PersonelHareket")))
{
ws.Cells[mRow, 1] = p.Attribute(XName.Get("PersonelID")).Value;
ws.Cells[mRow, 2] = p.Attribute(XName.Get("PersonelName")).Value;
ws.Cells[mRow, 3] = p.Attribute(XName.Get("ActionDate")).Value;
mRow++;
}
ws.Cells.Columns.AutoFit();
wb.SaveAs(pDosyaAdi, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value,
Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value);
wb.Close(true, Missing.Value, Missing.Value);
xlApp.Quit();
Logla(pDosyaAdi + ".xls oluşturuldu");
}
catch (Exception ex)
{
Logla("HATA : " + ex.Message);
}
finally
{
System.Threading.Thread.CurrentThread.CurrentCulture = myCultureInfo;
}
}
2 kişi tarafından 5.0 olarak değerlendirildi
- Currently 5/5 Stars.
- 1
- 2
- 3
- 4
- 5