Can use C# to create and save excel file. But can't reopen the file as it crashes

I

I Decker

Hi all,

Hope this is the right group.

I am writing a program in c# to open create an excel document, enter
some data, save it and then email it as an attachment. I have
successfully created an excel document which the user can see (at this
stage of development) and passed some data to it. I then used the
savas method to save the file. Again this seems to work as the file is
created. However once I close the excel file and try and reopen it
from the automatically saved version I get the following error
message, which results in it being impossible to open the excel doc.
This happens not just on my machine, but any computer that tries to
open these files.

Error message title bar reads as follows

Microsoft Excel – DebugMy Sheet.xls: Excel.EXE – Application Error

The body of the error message reads as follows

The instruction at "0x300045619" referenced memory at "0x00050018".
The memory could not be "read".
Click on OK to terminate the program
Click on Cancel to debug the program

I have research similar errors on Microsoft's site and have search
newsgroups. Although lots of people seem to get similar errors, they
seem to be caused by different situations and are not applicable to
what I am doing

I am using C# code and Excel 2000

I hope somebody has an answer

Here is my code
---------------------------------
//create excel file and data
Excel.Application ExcelApp;
Excel._Workbook ExcelWorkbook;
Excel._Worksheet ExcelWorkSheet;

ExcelApp = new Excel.Application();
ExcelApp.Visible = true;
ExcelWorkbook = (Excel._Workbook)(ExcelApp.Workbooks.Add(
Missing.Value ));

ExcelWorkSheet = (Excel._Worksheet)ExcelWorkbook.Sheets["Sheet1"];
ExcelWorkSheet.Name = "My Sheet";


//Make sure Excel is visible and give the user control
//of Microsoft Excel's lifetime.
ExcelApp.Visible = true;
ExcelApp.UserControl = true;


//text in cell
ExcelWorkSheet.Cells[1, 1] = "test sheet";
ExcelWorkSheet.Cells[3, 1] = "test1";
ExcelWorkSheet.Cells[3, 2] = "test2";
ExcelWorkSheet.Cells[3, 3] = "test3";
ExcelWorkSheet.Cells[3, 4] = "test4";
ExcelWorkSheet.Cells[3, 5] = "test5";
ExcelWorkSheet.Cells[3, 6] = " test6";

ExcelWorkSheet.get_Range("A3", "F3").HorizontalAlignment =
Excel.XlHAlign.xlHAlignCenter;

ExcelWorkSheet.get_Range("A1", "A1").Font.Bold = true;
ExcelWorkSheet.get_Range("A3", "F3").EntireColumn.AutoFit();
ExcelWorkSheet.get_Range("A3",F3").BorderAround(Excel.XlLineStyle.xlContinuous,Excel.XlBorderWeight.xlMedium,
Excel.XlColorIndex.xlColorIndexAutomatic,Excel.XlColorIndex.xlColorIndexAutomatic);

ExcelWorkSheet.get_Range("E1",E1").Columns.EntireColumn.NumberFormat =
"£###,###,##0.00";

string savepath;
savepath = Application.ExecutablePath.ToString();
savepath = savepath.Substring(0,savepath.LastIndexOf("\\"));
savepath = savepath + "\\TestExcelFile.xls";

ExcelWorkbook.SaveAs(savepath,Excel.XlFileFormat.xlCSV,Type.Missing,Type.Missing,Type.Missing,
Type.Missing,Excel.XlSaveAsAccessMode.xlNoChange,Type.Missing,Type.Missing,
Type.Missing,Type.Missing);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top