0

I use following code and its working. But every time it create an excel sheet with same name.

Can anybody tell me how export excel sheet with same name as XML sheet has???????

    private void button2_Click(object sender, EventArgs e)
    {
        MSDN.Sample.XMLToExcel.OpenXMLOffice objTest = new MSDN.Sample.XMLToExcel.OpenXMLOffice();
        objTest.XMLToExcel(textBox14.Text);
        MessageBox.Show("Umpire Report Extracted successfully.");
    }
2
  • I don't understand your question at all. Please revise it. Commented Apr 23, 2013 at 4:21
  • see if this helps. Commented Apr 23, 2013 at 4:23

1 Answer 1

2

Your given sample uses DataSet.ReadXml to convert the XML file to a DataSet, and then uses the name of the DataSet (DataSet.DataSetName) for the file name, and the name of the table (DataTable.TableName) as the sheet name. ReadXml selects those names based the contents of the XML file. If you want to change that behavior, you have two options:

  1. Create a method that creates the Excel as you wish:

    Add this to OpenXMLOffice class:

    public void XMLToExcel(string strXMLPath, string outputFileName, string sheetName)
    {
        string strPath = Path.GetDirectoryName(strXMLPath);
        DataSet dsXML = new DataSet();
        dsXML.ReadXml(strXMLPath);
        strPath = strPath + "\\" + outputFileName + ".xlsx";
        DataTable tblXML = dsXML.Tables[0];
        tblXML.TableName = sheetName;
        DataTableToFilePath(tblXML, strPath);
    }
    

    and call it like this:

    var objTest = new MSDN.Sample.XMLToExcel.OpenXMLOffice();
    objTest.XMLToExcel(textBox14.Text, "My Excel", "My Sheet");
    
  2. Change the contents of the XML file:

    • Change the root node to change output file name.
    • Change the second level node names to change the sheet name.
Sign up to request clarification or add additional context in comments.

1 Comment

@ShrirangWyawahare If the answers helped you, you should mark it as accepted answer. This way you (and me) earn reputation, and also it helps other people with same problem. Please read FAQ for more information.

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.