5
Answers

Text to columns in excel with C# button press

Janis Peksa

Janis Peksa

13y
6.5k
1

Hello, I have a code whats inserts .txt document into excel and saves it and than opens the excel file.
How can I do on colum A text to columns function whan excel is already opened ?
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.Visible =
true;
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);
}
private void button2_Click(object sender, EventArgs e)
{
//what to do here ?
}
}
}
Answers (5)