Hello, I have a code thats copy from .txt file information to excel and saves it. But I can't open the excel file. Can any help me please ?
Here is my code:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;
namespaceWindowsFormsApplication1
{
publicpartialclassForm1 : Form
{
publicForm1()
{
InitializeComponent();
}
private Excel.Application m_objExcel = null;
private Excel.Workbooks m_objBooks = null;
private Excel._Workbook m_objBook = null;
privateobject m_objOpt = System.Reflection.Missing.Value;
privateobject m_strSampleFolder = "C:\\Documents and Settings\\JPeksa\\Desktop\\";
privatevoidbutton1_Click(object sender, EventArgs e)
{
m_objExcel = new Excel.Application();
m_objBooks = (Excel.Workbooks)m_objExcel.Workbooks;
m_objBooks.OpenText(m_strSampleFolder + "08.2010 - Augusts - Ericsson.txt", Excel.XlPlatform.xlWindows, 1,
Excel.XlTextParsingType.xlDelimited, Excel.XlTextQualifier.xlTextQualifierDoubleQuote,
false, true, false, false, false, false, m_objOpt, m_objOpt,
m_objOpt, m_objOpt, m_objOpt);
m_objBook = m_objExcel.ActiveWorkbook;
m_objBook.SaveAs(m_strSampleFolder + "Book6.xls", Excel.XlFileFormat.xlWorkbookNormal,
m_objOpt, m_objOpt, m_objOpt, m_objOpt, Excel.XlSaveAsAccessMode.xlNoChange, m_objOpt, m_objOpt,
m_objOpt, m_objOpt);
m_objBook.Close(false, m_objOpt, m_objOpt);
m_objExcel.Quit();
m_objExcel.Workbooks.Open(m_strSampleFolder + "Book6.xls", Type.Missing, true, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
}
}
}
4 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Janis Peksa145 reputationPosted Nov 29, 2010, 2:40 AM EST
0
Someone help please.
0
Suthish NairRank #92 · 22.8K reputationPosted Nov 29, 2010, 5:52 AM EST
0
Janis,
The problem might, you still already have the excel reference/object in memory.
Open task manager, remove any excel.exe process already existing and try.
Kumar AG48 reputationPosted Nov 29, 2010, 6:51 AM EST
m_objExcel.Quit();
with
m_objExcel.Visible = true;
Janis Peksa145 reputationPosted Nov 29, 2010, 2:40 AM EST
Suthish NairRank #92 · 22.8K reputationPosted Nov 29, 2010, 5:52 AM EST
The problem might, you still already have the excel reference/object in memory.
Open task manager, remove any excel.exe process already existing and try.
Use before Open,
GC.Collect(); GC.WaitForPendingFinalizers();
Janis Peksa145 reputationPosted Nov 29, 2010, 9:07 PM EST
Thank you it helped :)