2011-03-04 109 views
1

如何用excel工作簿中的預定義模式創建xml?將excel轉換爲xml

+0

你有沒有任何編程語言或框架? – 2011-03-04 10:43:51

+0

c#與.net框架 – harsh 2011-03-04 10:58:38

回答

0

有一招。您可以創建XSL(樣式表)將您的XML轉換爲簡單的HTML標記。使用XslCompiledTransform.Transform()進行轉換並使用XLS(Excel電子表格)擴展名進行保存。它將通過MS Excel打開,並且所有Excel功能都將可用。據我所知,如果您真的需要快速轉儲數據,以便可以在Excel中打開,那麼這是最佳解決方案。

+0

我想將excel轉換爲xml – harsh 2011-03-04 12:10:12

0

這是一個可能幫助你的功能,我沒有那麼瞭解你的問題。簡單地把你的文件名和xls與數據集中的數據一起生成。

public static void exportToExcel(DataSet source, string fileName) 
    { 

     System.IO.StreamWriter excelDoc; 

     excelDoc = new System.IO.StreamWriter(fileName); 
     const string startExcelXML = "<xml version>\r\n<Workbook " + 
       "xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"\r\n" + 
       " xmlns:o=\"urn:schemas-microsoft-com:office:office\"\r\n " + 
       "xmlns:x=\"urn:schemas- microsoft-com:office:" + 
       "excel\"\r\n xmlns:ss=\"urn:schemas-microsoft-com:" + 
       "office:spreadsheet\">\r\n <Styles>\r\n " + 
       "<Style ss:ID=\"Default\" ss:Name=\"Normal\">\r\n " + 
       "<Alignment ss:Vertical=\"Bottom\"/>\r\n <Borders/>" + 
       "\r\n <Font/>\r\n <Interior/>\r\n <NumberFormat/>" + 
       "\r\n <Protection/>\r\n </Style>\r\n " + 
       "<Style ss:ID=\"BoldColumn\">\r\n <Font " + 
       "x:Family=\"Swiss\" ss:Bold=\"1\"/>\r\n </Style>\r\n " + 
       "<Style  ss:ID=\"StringLiteral\">\r\n <NumberFormat" + 
       " ss:Format=\"@\"/>\r\n </Style>\r\n <Style " + 
       "ss:ID=\"Decimal\">\r\n <NumberFormat " + 
       "ss:Format=\"0.0000\"/>\r\n </Style>\r\n " + 
       "<Style ss:ID=\"Integer\">\r\n <NumberFormat " + 
       "ss:Format=\"0\"/>\r\n </Style>\r\n <Style " + 
       "ss:ID=\"DateLiteral\">\r\n <NumberFormat " + 
       "ss:Format=\"mm/dd/yyyy;@\"/>\r\n </Style>\r\n " + 
       "</Styles>\r\n "; 
     const string endExcelXML = "</Workbook>"; 

     int rowCount = 0; 
     int sheetCount = 1; 
     /* 
     <xml version> 
     <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" 
     xmlns:o="urn:schemas-microsoft-com:office:office" 
     xmlns:x="urn:schemas-microsoft-com:office:excel" 
     xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"> 
     <Styles> 
     <Style ss:ID="Default" ss:Name="Normal"> 
     <Alignment ss:Vertical="Bottom"/> 
     <Borders/> 
     <Font/> 
     <Interior/> 
     <NumberFormat/> 
     <Protection/> 
     </Style> 
     <Style ss:ID="BoldColumn"> 
     <Font x:Family="Swiss" ss:Bold="1"/> 
     </Style> 
     <Style ss:ID="StringLiteral"> 
     <NumberFormat ss:Format="@"/> 
     </Style> 
     <Style ss:ID="Decimal"> 
     <NumberFormat ss:Format="0.0000"/> 
     </Style> 
     <Style ss:ID="Integer"> 
     <NumberFormat ss:Format="0"/> 
     </Style> 
     <Style ss:ID="DateLiteral"> 
     <NumberFormat ss:Format="mm/dd/yyyy;@"/> 
     </Style> 
     </Styles> 
     <Worksheet ss:Name="Sheet1"> 
     </Worksheet> 
     </Workbook> 
     */ 
     excelDoc.Write(startExcelXML); 
     excelDoc.Write("<Worksheet ss:Name=\"Sheet" + sheetCount + "\">"); 
     excelDoc.Write("<Table>"); 
     excelDoc.Write("<Row>"); 
     for (int x = 0; x < source.Tables[0].Columns.Count; x++) 
     { 
      excelDoc.Write("<Cell ss:StyleID=\"BoldColumn\"><Data ss:Type=\"String\">"); 
      excelDoc.Write(source.Tables[0].Columns[x].ColumnName); 
      excelDoc.Write("</Data></Cell>"); 
     } 
     excelDoc.Write("</Row>"); 
     foreach (DataRow x in source.Tables[0].Rows) 
     { 
      rowCount++; 
      //if the number of rows is > 64000 create a new page to continue output 

      if (rowCount == 64000) 
      { 
       rowCount = 0; 
       sheetCount++; 
       excelDoc.Write("</Table>"); 
       excelDoc.Write(" </Worksheet>"); 
       excelDoc.Write("<Worksheet ss:Name=\"Sheet" + sheetCount + "\">"); 
       excelDoc.Write("<Table>"); 
      } 
      excelDoc.Write("<Row>"); //ID=" + rowCount + " 

      for (int y = 0; y < source.Tables[0].Columns.Count; y++) 
      { 
       System.Type rowType; 
       rowType = x[y].GetType(); 
       switch (rowType.ToString()) 
       { 
        case "System.String": 
         string XMLstring = x[y].ToString(); 
         XMLstring = XMLstring.Trim(); 
         XMLstring = XMLstring.Replace("&", "&"); 
         XMLstring = XMLstring.Replace(">", ">"); 
         XMLstring = XMLstring.Replace("<", "<"); 
         excelDoc.Write("<Cell ss:StyleID=\"StringLiteral\">" + 
             "<Data ss:Type=\"String\">"); 
         excelDoc.Write(XMLstring); 
         excelDoc.Write("</Data></Cell>"); 
         break; 
        case "System.DateTime": 
         //Excel has a specific Date Format of YYYY-MM-DD followed by 

         //the letter 'T' then hh:mm:sss.lll Example 2005-01-31T24:01:21.000 

         //The Following Code puts the date stored in XMLDate 

         //to the format above 

         DateTime XMLDate = (DateTime)x[y]; 
         string XMLDatetoString = ""; //Excel Converted Date 

         XMLDatetoString = XMLDate.Year.ToString() + 
          "-" + 
          (XMLDate.Month < 10 ? "0" + 
          XMLDate.Month.ToString() : XMLDate.Month.ToString()) + 
          "-" + 
          (XMLDate.Day < 10 ? "0" + 
          XMLDate.Day.ToString() : XMLDate.Day.ToString()) + 
          "T" + 
          (XMLDate.Hour < 10 ? "0" + 
          XMLDate.Hour.ToString() : XMLDate.Hour.ToString()) + 
          ":" + 
          (XMLDate.Minute < 10 ? "0" + 
          XMLDate.Minute.ToString() : XMLDate.Minute.ToString()) + 
          ":" + 
          (XMLDate.Second < 10 ? "0" + 
          XMLDate.Second.ToString() : XMLDate.Second.ToString()) + 
          ".000"; 
         excelDoc.Write("<Cell ss:StyleID=\"DateLiteral\">" + 
            "<Data ss:Type=\"DateTime\">"); 
         excelDoc.Write(XMLDatetoString); 
         excelDoc.Write("</Data></Cell>"); 
         break; 
        case "System.Boolean": 
         excelDoc.Write("<Cell ss:StyleID=\"StringLiteral\">" + 
            "<Data ss:Type=\"String\">"); 
         excelDoc.Write(x[y].ToString()); 
         excelDoc.Write("</Data></Cell>"); 
         break; 
        case "System.Int16": 
        case "System.Int32": 
        case "System.Int64": 
        case "System.Byte": 
         excelDoc.Write("<Cell ss:StyleID=\"Integer\">" + 
           "<Data ss:Type=\"Number\">"); 
         excelDoc.Write(x[y].ToString()); 
         excelDoc.Write("</Data></Cell>"); 
         break; 
        case "System.Decimal": 
        case "System.Double": 
         excelDoc.Write("<Cell ss:StyleID=\"Decimal\">" + 
           "<Data ss:Type=\"Number\">"); 
         excelDoc.Write(x[y].ToString()); 
         excelDoc.Write("</Data></Cell>"); 
         break; 
        case "System.DBNull": 
         excelDoc.Write("<Cell ss:StyleID=\"StringLiteral\">" + 
           "<Data ss:Type=\"String\">"); 
         excelDoc.Write(""); 
         excelDoc.Write("</Data></Cell>"); 
         break; 
        default: 
         throw (new Exception(rowType.ToString() + " not handled.")); 
       } 
      } 
      excelDoc.Write("</Row>"); 
     } 
     excelDoc.Write("</Table>"); 
     excelDoc.Write(" </Worksheet>"); 
     excelDoc.Write(endExcelXML); 
     excelDoc.Close(); 
    } 
0

如果你在excel中有一些數據並想將它們轉換爲xml,那麼你應該有一個合適的XML Schema定義。我假設你有一個合適的模式,那麼你只需要遵循幾個簡單的步驟在Excel中將其轉換爲XML。讓我知道,如果你正在尋找如何在Excel中實現這個步驟...希望你有最新版本的微軟辦公室..