Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > reading from text file to excel file

Reply
Thread Tools

reading from text file to excel file

 
 
mail2atulmehta@yahoo.com
Guest
Posts: n/a
 
      04-12-2005
Hi,
I do not know if this is right place for this, but i need some help. I
have a text file, whose values are seprated by a delimiter. I want to
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.xlWBAT Worksheet);
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.XlFileFormat*.xlWorkbookNormal,

null,null,false,false,Excel.Xl*SaveAsAccessMode.xl Shared,fals*e,false,null,null);


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

null,null,false,false,Excel.Xl*SaveAsAccessMode.xl Shared,fals*e,false,null,null);

}
}


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

Console.WriteLine("{0}\n{1}",e*x.Message,ex.StackT race);
//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.ReleaseCom Object
(currentCell);

//System.Runtime.InteropServic*es.Marshal.ReleaseCom Object
(ExcelObj);

//System.Runtime.InteropServic*es.Marshal.ReleaseCom Object
(worksheet);

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


}
}

 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      04-12-2005
"" <> writes:
> I do not know if this is right place for this, but i need some help. I
> have a text file, whose values are seprated by a delimiter. I want to
> open this file in excel, ( not import it) . I have written the driver
> prg in c#.

[SNIP]

No, this isn't the right place. C and C# are two different languages;
we only know about one of them here. You probably want one of the
microsoft.* groups, or perhaps comp.os.ms-windows.programmer.

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with Excel reports ::::Excel 2003 Migration To Excel 2007 =?Utf-8?B?c2hhc2hhbmsga3Vsa2Fybmk=?= ASP .Net 15 10-24-2007 01:34 PM
ExtenXLS loads data into Excel 2002 file but damages the Excel file. kp2900@gmail.com Java 1 11-21-2006 05:48 PM
Reading excel file using Java Excel API pps! Java 0 08-29-2006 12:05 PM
exporting an excel file from database; making changes to excel file and updating the database by importing it back Luis Esteban Valencia ASP .Net 1 01-12-2005 12:28 AM
reading the DB vs. reading a text file...performance preference? Darrel ASP .Net 3 11-11-2004 02:27 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57