Janis Peksa

Janis Peksa

  • NA
  • 152
  • 50.7k

Open excel using C#

Nov 29 2010 6:22 AM

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:
 

 
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;
namespace
WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Excel.Application m_objExcel = null;
private Excel.Workbooks m_objBooks = null;
private Excel._Workbook m_objBook = null;
private object m_objOpt = System.Reflection.Missing.Value;
private object m_strSampleFolder = "C:\\Documents and Settings\\JPeksa\\Desktop\\";
private void button1_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);
}
}
}

Answers (4)