W
wasishincar
Hi all,
I wrote a small app, which could read text file and fill the data from
text file to a newly created excel file. It works fine on my computer,
but did not succeed on someone's. I caught an exception which told it
occurred when "trying to read or write protected memory. It usually
means the memory was damaged". What did I need to check? The account is
administrator, and it should be fine to write file at any place in that
PC.
Here is a my code. Thanks for any replying.
Excel.Application ExcelObj = new
Microsoft.Office.Interop.Excel.Application();
void func1(string xlsfilename)
{
// See if the Excel Application Object was successfully constructed
if (ExcelObj == null)
{
MessageBox.Show("ERROR: EXCEL couldn't be started!");
return;
}
try
{
string filename = xlsfilename;
Excel.Workbook wb = ExcelObj.Workbooks.Add(Type.Missing);
// get the collection of sheets in the workbook
Excel.Sheets sheets = wb.Worksheets;
Excel.Worksheet worksheet;
worksheet = (Excel.Worksheet)sheets.get_Item(1);
worksheet.Name = "Data";
//do some data processing
wb.SaveAs( filename , Type.Missing, Type.Missing, Type.Missing,
Type.Missing,
Type.Missing,
Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing,
Type.Missing, Type.Missing, Type.Missing,
Type.Missing);
wb.Close(false, Type.Missing, false);
}
catch(Exception ex)
{
MessageBox.Show("Message: " + ex.Message + "\nSource: " + ex.Source
+ "\nTrack: "
+ ex.StackTrace);
}
ExcelObj.Workbooks.Close();
ExcelObj.Quit();
ExcelObj = null;
GC.Collect();
}
I wrote a small app, which could read text file and fill the data from
text file to a newly created excel file. It works fine on my computer,
but did not succeed on someone's. I caught an exception which told it
occurred when "trying to read or write protected memory. It usually
means the memory was damaged". What did I need to check? The account is
administrator, and it should be fine to write file at any place in that
PC.
Here is a my code. Thanks for any replying.
Excel.Application ExcelObj = new
Microsoft.Office.Interop.Excel.Application();
void func1(string xlsfilename)
{
// See if the Excel Application Object was successfully constructed
if (ExcelObj == null)
{
MessageBox.Show("ERROR: EXCEL couldn't be started!");
return;
}
try
{
string filename = xlsfilename;
Excel.Workbook wb = ExcelObj.Workbooks.Add(Type.Missing);
// get the collection of sheets in the workbook
Excel.Sheets sheets = wb.Worksheets;
Excel.Worksheet worksheet;
worksheet = (Excel.Worksheet)sheets.get_Item(1);
worksheet.Name = "Data";
//do some data processing
wb.SaveAs( filename , Type.Missing, Type.Missing, Type.Missing,
Type.Missing,
Type.Missing,
Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing,
Type.Missing, Type.Missing, Type.Missing,
Type.Missing);
wb.Close(false, Type.Missing, false);
}
catch(Exception ex)
{
MessageBox.Show("Message: " + ex.Message + "\nSource: " + ex.Source
+ "\nTrack: "
+ ex.StackTrace);
}
ExcelObj.Workbooks.Close();
ExcelObj.Quit();
ExcelObj = null;
GC.Collect();
}