0

This can be a very naive question. I want to write into into an excel file and each time the insert of data should happen on a new line. Here is what i have to do in detail:

  1. Create an Excel Dynamically and name it on the basis of the current date.
  2. Add the headers like "Actual", "Expected " and Outcome.
  3. insert date in the above columns .

I have a small code which verifies certain fields , so I wan to write into excel the fields which deviate from the expected behavior. So each run whenever my code finds an error it should write into that excel.

1

3 Answers 3

2

It is possible to write to an excel file without using any third-party library by using the Excel Spreadsheet XML format. All you need is using a XmlTextWriter. Here is an example (the stream where to write the excel is assumed to be provided):

XmlTextWriter w = new XmlTextWriter(stream, null); // Creates the XML writer from pre-declared stream.

//First Write the Excel Header
w.WriteStartDocument();
w.WriteProcessingInstruction("mso-application", "progid='Excel.Sheet'");

w.WriteStartElement("Workbook");
w.WriteAttributeString("xmlns", "urn:schemas-microsoft-com:office:spreadsheet");
w.WriteAttributeString("xmlns", "o", null, "urn:schemas-microsoft-com:office:office");
w.WriteAttributeString("xmlns", "x", null, "urn:schemas-microsoft-com:office:excel");
w.WriteAttributeString("xmlns", "ss", null, "urn:schemas-microsoft-com:office:spreadsheet");
w.WriteAttributeString("xmlns", "html", null, "http://www.w3.org/TR/REC-html40");

w.WriteStartElement("DocumentProperties");
w.WriteAttributeString("xmlns", "urn:schemas-microsoft-com:office:office");
w.WriteEndElement();

// Creates the workbook
w.WriteStartElement("ExcelWorkbook");
w.WriteAttributeString("xmlns", "urn:schemas-microsoft-com:office:excel");
w.WriteEndElement();

// Creates the worksheet
w.WriteStartElement("Worksheet");
w.WriteAttributeString("ss", "Name", null, "Sheet1");

// Creates the table
w.WriteStartElement("Table");

// Creates a row.
w.WriteStartElement("Row");

// Creates a cell with "SomeData" written in it.
w.WriteStartElement("Cell");
w.WriteStartElement("Data");
w.WriteAttributeString("ss", "Type", null, "String");
w.WriteString("SomeData");
w.WriteEndElement();
w.WriteEndElement();

w.WriteEndElement(); // Closes the row.

w.WriteEndElement();
w.WriteEndElement();
w.WriteEndElement();
w.WriteEndDocument();
w.Flush();
w.Close();
2

You would want to use Closed XML which a wrapper around Open XML SDK. Check out their examples.

If you do not want to rely on a 3rd party library you can use Open XML SDK directly.

6
  • ClosedXML does have some nasty bugs, though. The ones I found included: 1- When opening an excel file, setting a cell's color, then saving; this could lead to any new excel files having their default color set to anything other than white. 2- When writing "Hello\nWorld" in a cell, then auto-adjust the row sizes, it will not auto-adjust, so you'll only see "Hello"; it does work, when writing "Hello\r\nWorld" though. 3- Even though you call sheet.AddToNamed, sheet.NamedRanges will remain empty, so you need to use sheet.WorkBook.NamedRanges to retrieve the NamedRanges instead (odd, right?).
    – Nolonar
    Commented May 22, 2013 at 9:17
  • @Nolonar : I suggested Closed XML because it's easy to get it up and running. The alternative is to write Open XML Code which has a higher learning curve and involves a fair bit of coding.
    – cvraman
    Commented May 22, 2013 at 9:24
  • I know, and I upvoted this answer as well. I just thought it might be appropriate to mention it, in case the OP runs into those bugs and doesn't know how to deal with them. If you found my comment disturbing, I can delete it.
    – Nolonar
    Commented May 22, 2013 at 9:26
  • @Nolonar : No need to delete. It's good to know what you are getting into.
    – cvraman
    Commented May 22, 2013 at 9:27
  • I think we should be able to write some simple code rather then using 3rd party stuff. Commented May 22, 2013 at 9:35
0

You can use the following ways to write into excel file:

  1. Use COM instance of Excel application
  2. Use a third party component to write excel

I use syncfusion xslIo component to read and write excel files

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.