PDA

View Full Version : export listbox to excel



Mainar
شنبه 26 دی 1388, 10:32 صبح
سلام
من براي export از Listbox به Excel از اين كد استفاده مي كنم:




try
{
Microsoft.Office.Interop.Excel.Application excelapp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook excelbook = excelapp.Workbooks.Add(XlSheetType.xlWorksheet);
Microsoft.Office.Interop.Excel.Worksheet excelworksheet = (Worksheet)(excelbook.Worksheets[1]);
excelworksheet.DisplayRightToLeft = true;
for (int j = 0; j < listBox1.Items.Count; j++)
{
Range da = excelworksheet.get_Range("A1", "A1");
rng1.Value2 = "©§یه";
rng1.Font.Bold = true;
}
ProgressBar1.Maximum = listBox1.Items.Count;
ProgressBar1.Visible = true;
for (int i = 2; i < listBox1.Items.Count; i++)
{
string s = i.ToString();
Range r1 = excelworksheet.get_Range("A" + s, "A" + s);
r1.Value2 = listBox1.Items[0].ToString();
Range r2 = excelworksheet.get_Range("B" + s, "B" + s);
r2.Value2 = listBox1.Items[1].ToString();
Range r3 = excelworksheet.get_Range("C" + s, "C" + s);
r3.Value2 = listBox1.Items[2].ToString();
Range r4 = excelworksheet.get_Range("D" + s, "D" + s);
r4.Value2 = listBox1.Items[3].ToString();
Range r5 = excelworksheet.get_Range("E" + s, "E" + s);
r5.Value2 = listBox1.Items[4].ToString();
//excelapp.Save("d:\\test.xls");
excelapp.WorkBooks[1].SaveCopyAs("C:\\test.xls");
//ProgressBar1.Value += 1;
}
ProgressBar1.Visible = false;
excelapp.Visible = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}




ولي موقع اجرا روي خط دوم اين error رو ميده:


Old format or invalid type library. (Exception from HRESULT: 0x80028018 (TYPE_E_INVDATAREAD))

ehsan2007
شنبه 26 دی 1388, 12:24 عصر
سلام
regional setting ویندوز خود را به english تغییر دهید. مشکل شما حل میشه

Afshin160
چهارشنبه 30 دی 1388, 08:41 صبح
و بهتر که قبل کد خودت کد زیر را بنویسی و دیگه احتیاج نداری که دستی تنظیمات ویندوز را عوض کنی




//Error: 0x80028018 (-2147647512)
//Description: Old Format or Invalid Type Library
//* The method requires an LCID (locale identifier).
//* You run an English version of Excel. However, the regional settings for the computer are configured for a non-English language.
// برای برطرف کردن دو خط کد زیر را باید بنویسم و یا از کنترل پنل تنظیمات را درست کنیم


System.Globalization.CultureInfo orgCI = System.Threading.Thread.CurrentThread.CurrentCultu re;
System.Threading.Thread.CurrentThread.CurrentCultu re = new System.Globalization.CultureInfo("en-US");