Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default reading from text file to excel file


Hi,
I have a text file, whose values are seprated by a delimiter. I want t

open this file in excel, ( not import it) . I have written the driver
prg in c#. The code opens the file, but it is not writting the values
from text file into excel file. I can not figure out the problem here

This is my code:
StreamReader dataFileReader = null;
FileInfo file = null;
Excel.Application ExcelObj = new Excel.Application();
Excel.Workbook workbook = null;
Excel.Range currentCell = null;
Excel.Worksheet worksheet = null;
string txtoutput = null;
Excel.Sheets excelSheets = null;
Excel.Range sampleResults = null;


}


public void transformation()
{


try
{
//GC.Collect();
workbook =
ExcelObj.Workbooks.Add(Excel.X**lWBATemplate.xlWBA TWorksheet)*;
excelSheets = workbook.Worksheets;
worksheet =
(Excel.Worksheet)excelSheets.g**et_Item(1);
if (worksheet == null)
{
Console.WriteLine ("ERROR in
worksheet == null");
}
// Opens the text file.
string inFile ="C:\\dip\\output2.txt"

using (StreamReader reader = new
StreamReader(inFile) )
{
String currentLine;
sampleResults =


worksheet.get_Range("A1", Type.Missing);
int row = 1;
int count = 0;
// Populates the table of
sample results.
// Continues reading lines
until there are no more.
while ((currentLine =
reader.ReadLine()) != null)
{
count++;


Console.WriteLine("Reading string # {0} to populate excel row #
{1}", count, count);
string[] textInfo =
currentLine.Split(',');
// Places each sample
result into the appropriate cell.
for (int i = 0; i <
textInfo.Length; i++)
{
currentCell =


(Excel.Range)sampleResults.Cel**ls[row, i+1];


currentCell.Value2 =
textInfo[i].Trim();
}
row++;
currentLine =
reader.ReadLine();
}


//thisWorkbook.SaveAs("C://dip**//test.xls",Excel.XlFileForma*t*.xlWorkbookNormal



null,null,false,false,Excel.Xl**SaveAsAccessMode.x lShared,fal*s*e,false,null,null)



workbook.SaveAs("C://dip//test**.xls",Excel.XlFileFormat.xlWo*r*kbookNormal



null,null,false,false,Excel.Xl**SaveAsAccessMode.x lShared,fal*s*e,false,null,null)



}
}


catch(Exception ex)
{
//Debug.WriteLine(ex.Message +
ex.StackTrace);


Console.WriteLine("{0}\n{1}",e**x.Message,ex.Stack Trace);
//Thread.Sleep(3000);
}
finally
{
//excelTW.Close();
//dataFileReader.Close();
//delete files for one time usage
//File.Delete(output2);
//close resources, allocated for the
Excel process
workbook.Close(false, Type.Missing,
Type.Missing);
ExcelObj.Workbooks.Close();
ExcelObj.Quit();


//System.Runtime.InteropServic**es.Marshal.ReleaseCo mObject
(currentCell);


//System.Runtime.InteropServic**es.Marshal.ReleaseCo mObject
(ExcelObj);


//System.Runtime.InteropServic**es.Marshal.ReleaseCo mObject
(worksheet);


//System.Runtime.InteropServic**es.Marshal.ReleaseCo mObject
(workbook);
worksheet=null;
workbook=null;
ExcelObj = null;
//try to force GC
GC.Collect();


}
}

Thank

--
dgoe
-----------------------------------------------------------------------
dgoel's Profile: http://www.msusenet.com/member.php?userid=48
View this thread: http://www.msusenet.com/t-186943009

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA macro for reading text file into Excel Javed Khan Excel Discussion (Misc queries) 0 October 14th 09 06:29 PM
Reading from a text file Jeff Excel Discussion (Misc queries) 1 November 8th 06 08:47 PM
Reading a text file ? WTG Excel Worksheet Functions 2 February 22nd 05 01:29 AM
Reading a text file WTG Excel Discussion (Misc queries) 2 February 22nd 05 01:27 AM
Help reading in a text file Mike[_60_] Excel Programming 1 January 21st 04 06:29 PM


All times are GMT +1. The time now is 02:44 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"