p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/)
-   VS.NET 2002/2003 (http://p2p.wrox.com/vs-net-2002-2003-103/)
-   -   Exporting listview data to excel file (http://p2p.wrox.com/vs-net-2002-2003/29113-exporting-listview-data-excel-file.html)

dotnetprogrammer May 10th, 2005 02:37 AM

Exporting listview data to excel file
 
Hi!

I am using the following code to create a excel file. I have office 2003 installed in my machine. I have made following reference Microsoft excel 11.0 object library. The code is working fine in my machine. But in other machine (where office 2000 is installed) It is giving object reference not set to instance of object exception in the following line.

oWB.SaveAs(strfilename ,Ex.XlFileFormat.xlWorkbookNormal, null, null,false,
false, Ex.XlSaveAsAccessMode.xlShared, false, false, null,
null, null);


using Ex=Microsoft.Office.Interop.Excel;


Ex.Application oXL;
Ex._Workbook oWB;
Ex._Worksheet oSheet;
oXL = new Ex.Application();

oXL.Visible = false;
oWB= (Ex._Workbook)(oXL.Workbooks.Add( Missing.Value ));
oSheet = (Ex._Worksheet)oWB.ActiveSheet;
//oSheet.Columns
oSheet.Cells[1,1]="A";
oSheet.Cells[1,2]="B";
oSheet.Cells[1,3]="C";
oSheet.Cells[1,4]="D";
oSheet.Cells[1,5]="E";
oSheet.Cells[1,6]="F";
((Ex.Range)oSheet.Cells[1,1]).ColumnWidth = 20;
((Ex.Range)oSheet.Cells[1,2]).ColumnWidth = 20;
((Ex.Range)oSheet.Cells[1,3]).ColumnWidth = 20;
((Ex.Range)oSheet.Cells[1,4]).ColumnWidth = 40;
((Ex.Range)oSheet.Cells[1,5]).ColumnWidth = 20;
((Ex.Range)oSheet.Cells[1,6]).ColumnWidth = 20;
((Ex.Range)oSheet.Cells[1,1]).EntireRow.Font.Bold = true;
for(int i=0 ; i<FsciListView.Items.Count ; i++)
{
for(int j=1; j<=6 ; j++)
{
string str = listView1.Items[i].SubItems[j].Text;
oSheet.Cells[i+2,j] = str;
}
}
string strfilename = "C:\\tmp\\test.xls";
oWB.SaveAs(strfilename ,Ex.XlFileFormat.xlWorkbookNormal, null, null,false,
false, Ex.XlSaveAsAccessMode.xlShared, false, false, null,
null, null);
// Below line will kill the unkilled Excel process
System.Diagnostics.Process[] pProcess;
pProcess = System.Diagnostics.Process.GetProcessesByName("Exc el");
pProcess[0].Kill();
oSheet= null;
oWB = null;
oXL = null;


Please help me
I am really stuck in this

Regards
Saurabh

dotnet

Prashant.k.m May 10th, 2005 03:15 AM

hi,

There is chance of changes in api of excel 2000 & 2003 so you may have to create the introp file for 2000 which may be compatible for 2003, but in usualy backward compatiblity may not be there for some api.

Another thing,Do you have tmp folder in that machine also?

Prashant


dotnetprogrammer May 10th, 2005 09:13 AM

Yes I have tmp folder in that machine also.
Could you plz give me some code that will work in every version of outlook.

Regards

Saurabh

dotnet


All times are GMT -4. The time now is 11:45 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
2013 John Wiley & Sons, Inc.