Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
360 views
in Technique[技术] by (71.8m points)

c# - How to convert XML file to xls using DocumentFormat.OpenXml in asp.net?

How to convert XML file to xls using DocumentFormat.OpenXml in asp.net?

  • open the xml file from fileupload and then convert to.xls.
  • Convert very fast even though huge xml file.
  • No need to use Interop.Excel.Don't provide links, kindly explain with c# code.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

What you ask it's a sort of tutorial on using OpenXMLSdk and it's a bit out of topic i think ;-).

Anyway this example creates a xlsx file from scratch. Don't forget to download Xml sdk productivity tool.

Consider this xml but should work even with larger and more complex:

<?xml version="1.0" standalone="yes"?>
<NewDataSet>
  <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="TestTable" msdata:UseCurrentLocale="true">
      <xs:complexType>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element name="TestTable">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="pk" type="xs:long" minOccurs="0" />
                <xs:element name="Name" type="xs:string" minOccurs="0" />
                <xs:element name="Surname" type="xs:string" minOccurs="0" />
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:choice>
      </xs:complexType>
    </xs:element>
  </xs:schema>
  <TestTable>
    <pk>1</pk>
    <Name>Name1</Name>
    <Surname>Surname1</Surname>
  </TestTable>
  <TestTable>
    <pk>2</pk>
    <Name>Name2</Name>
    <Surname>Surname2</Surname>
  </TestTable>
  <TestTable>
    <pk>3</pk>
    <Name>Name3</Name>
    <Surname>Surname3</Surname>
  </TestTable>
  <TestTable>
    <pk>4</pk>
    <Name>Name4</Name>
    <Surname>Surname4</Surname>
  </TestTable>
  <TestTable>
    <pk>5</pk>
    <Name>Name5</Name>
    <Surname>Surname5</Surname>
  </TestTable>
  <TestTable>
    <pk>6</pk>
    <Name>Name6</Name>
    <Surname>Surname6</Surname>
  </TestTable>
</NewDataSet> 

Now you just need to call this:

        DataTable dt = new DataTable("TestTable");
        dt.ReadXml("c:\test.xml");

        ExcelNameSpace.ExcelFile xlsxFile = new ExcelNameSpace.ExcelFile(dt);
        xlsxFile.CreatePackage("c:\myFile.xlsx");

The code in the class ExcelNameSpace.ExcelFile at 90% it's reflected by productivity tool in a couple of seconds. You should be able to debug step by step and understand what are your needs. It's about 1000 lines so i can't post here. (http://www.devnmore.com/share/ExcelFromScratch.txt)

Hope it helps.

Francesco


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...