2

I am writing a program in C# for analysing a structure details in an earth quake.I have a lot of data and want to put them in a excel file. I mean i need a function like this :

public void excel(int sheet_no,int row,int column,string value)

Is there any way to parse the text file and Put this data in Excel sheet ?

4
  • 2
    What exactly do you want to do? Commented Jun 28, 2012 at 4:39
  • Extreeeemely vague. What are you trying to do, and what have you already done towards that end. Commented Jun 28, 2012 at 4:42
  • I have txt file with lots of numbers , over 1 milion detailed numbers , and want to put them in a excel file. that s all.How can i write them into excel file with C#? Commented Jun 28, 2012 at 4:46
  • Please have a look on following link: [ transfer data to an Excel workbook](support.microsoft.com/kb/306023) Convert the text file to Excel Format Commented Jun 28, 2012 at 5:00

3 Answers 3

2

use this :

Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
Workbooks workbooks;
_Workbook workbook;
_Workbook workbook2;
Sheets sheets;
_Worksheet worksheet;
app.Visible = false;
workbooks = app.Workbooks;
workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet);
sheets = workbook.Worksheets;
worksheet = (_Worksheet)sheets.get_Item(1);
worksheet.Cells[row, column] = value;
workbook.Saved = true;
workbook.SaveAs(output_file);
app.UserControl = false;
app.Quit();
Sign up to request clarification or add additional context in comments.

Comments

2

Take a look at ClosedXml

Code sample:

var workbook = new XLWorkbook();
var worksheet = workbook.Worksheets.Add("Sample Sheet");
worksheet.Cell("A1").Value = "Hello World!";
workbook.SaveAs("HelloWorld.xlsx");

But if you are dealing with a lot of data, consider using a database like sqlserver. It provides a lot of analysis tools. You can connect it as a datasource to excel too.

2 Comments

I Tested this code and it worked but there was a problem.I couldnt create new excel file.i had to use one ready excel file.you can test it your self.
@Abadis, look at link msdn.microsoft.com/en-us/library/… to create Workbooks and link msdn.microsoft.com/en-us/library/… to Open Workbooks. Basically you have to read up on Excel API and start understanding them. Sometimes MSDN doc provides code samples.
1

For now, I am providing links for you to read @ Excel Tasks and code samples @ Excel controls in ASP.net. If you're still confused, I may be able to find code samples from my projects or the Internet, which are not hard to find.

Look at link Workbook Open (create) method to create Workbooks, and link Workbook Open method to open Workbooks.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.