473,383 Members | 1,868 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

populate a excel sheet from text file


Hi,
I Have a text file & I want to open it in excel sheet ( withou
importing). I have written code for it, but it is not opening exce
sheet. It opens the text file, but does not create a excel sheet
populate it. I do not know what the problem is, can someone please hel
out with this one,
Here 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

Nov 17 '05 #1
0 2818

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Allison Bailey | last post by:
Hi Folks, I'm a brand new Python programmer, so please point me in the right direction if this is not the best forum for this question.... I would like to open an existing MS Excel spreadsheet...
3
by: Mads Petersen | last post by:
I'm stuck in this code. Hope you can and will help me. I launch it from excel. I have made the following code work, but not as i whant. I need the ranges to be working with something like xlDown....
3
by: Conrad F | last post by:
Hello All, I know how to import a specific named excel sheet into a datagrid using ADO.NET by setting up a JET connection and then SELECTing data from the sheet. However, for a real world...
3
by: krallabandi | last post by:
Hi, I am trying to generate Excel sheet using Provider=Microsoft.Jet.OLEDB.4.0; I am always getting the error while inserting data into any cell other than A. An unhandled exception of...
12
by: kath | last post by:
How do I read an Excel file in Python? I have found a package to read excel file, which can be used on any platform. http://www.lexicon.net/sjmachin/xlrd.htm I installed and working on the...
0
by: Padma Athimoolam | last post by:
Hi, I am new to VB and XML.I want to populate XML Values in Excel Sheet.My Sample XML is <?xml version="1.0" encoding="UTF-8" ?> - <workbook> - <worksheet name="Scorecard_KTC_MW"> <cell...
2
by: pulavarthipraveen | last post by:
Overview: We have a requirement in the c#.NET 1.0 windows application. There will be some input text file in the user’s machine. The user should browse and select the input text file and also select...
7
by: TG | last post by:
hi! I am trying to create a sql server table from an excel sheet. Here is the code I have: 'This procedure the xlsx file and dumps it to a table in SQL Server
2
hemantbasva
by: hemantbasva | last post by:
Note We need to have a template on server for generating report in multiple sheet as we do not had msoffice on server moreover this require a batch job to delete excel file created by the...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.