View Single Post
  #1 (permalink)  
Old May 10th, 2005, 02:37 AM
dotnetprogrammer dotnetprogrammer is offline
Authorized User
 
Join Date: Oct 2004
Location: delhi, delhi, India.
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to dotnetprogrammer
Default 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
__________________
dotnet
Reply With Quote