Okay so I have a program that calculates the time it takes for my application to run and i need it to enter those times in one row in excel 2007. What is the code or way for doing this? There are 1000 times iteration so you can see why me entering it manually is painful. I stopped at 36 :)


Thanks ahead of time

Writing directly to an Excel file is possible but difficult.
Why not write a simple text format file (eg comma or tab delimited) that Excel can import? That's really easy

If you are using Visual Studio, I believe there are some project templates that aid in this. However, I have never used them. I have had some experience with office and Interop. You will have to add a reference to Microsoft.Office.Interop.Excel (with the correct assembly version; 12 I believe).

using namespace Excel = Microsoft.Office.Interop.Excel;
...
public static void Main(string[] args)
{
    Excel.Application app = new Excel.Application();
    Excel.Workbook workbook = app.Workbooks.Open("testing.xls");
    ...
}

Note that there are classes in this namespace, but C# will not work with these; stick with the interfaces. Also, if you are working in anything before .NET 4.0, you will have to use Type.Missing for all optional parameters, like:

Excel.Workbook workbook = app.Workbooks.Open("testing.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
Comments
Question was asked in Java section not C#, so do not post irrelevant stuff

Writing directly to an Excel file is possible but difficult.
Why not write a simple text format file (eg comma or tab delimited) that Excel can import? That's really easy

How can I do this? It sounds more feasible

Just open a PrintStream to a FileOutputStream and use the print(...) method to write your data as text to the file.
To import all the data in one column just write one value per line ("\n" after each value). To import all the data in one long row write a tab "\t" after each value instead. Excel will just read that file into a new spreadsheet for you.

Yes, I would second nmaillet's suggestion to try POI. You can probably find a simple example in the quick guide to get you started.

Edited 5 Years Ago by Ezzaral: n/a

This article has been dead for over six months. Start a new discussion instead.