Mainar
شنبه 26 دی 1388, 11: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))
من براي 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))