2009-07-29 85 views
0

在.NET C#中,我试图打开Excel模板,添加一些数据并将其保存为新文档。我正在尝试使用OpenXML文档格式。我似乎无法找到关于如何做到这一点的任何指导。好像所有关于如何将各种部分写入软件包的文档说明一样,但当您完成并想要保存时,我找不到要执行的操作。保存Excel 2007文档

任何人都知道我可以在哪里找到这些信息?我一定在错误地思考这个问题,因为我没有发现任何有用的东西,看起来很基本。

感谢

+1

的SpreadsheetML是_not_ OpemXML – 2009-07-29 19:12:45

回答

1

ExcelPackage作品是相当不错的。它没有被主要作者的工作,我不认为有一段时间,但它的论坛上有很多人关注任何问题。

  FileInfo template = new FileInfo(Path.GetDirectoryName(Application.ExecutablePath)+"\\Template.xlsx"); 
     try 
     { 
      using (ExcelPackage xlPackage = new ExcelPackage(strFileName,template)) 
      { 
       //Enable DEBUG mode to create the xl folder (equlivant to expanding a xlsx.zip file) 
       //xlPackage.DebugMode = true; 

       ExcelWorksheet worksheet = xlPackage.Workbook.Worksheets["Sheet1"]; 

       worksheet.Name = WorkSheetName; 

       foreach (DataRow row in dt.Rows) 
       { 
        int c = 1; 
        if (r > startRow) worksheet.InsertRow(r); 
        // our query has the columns in the right order, so simply 
        // iterate through the columns 
        foreach (DataColumn col in dt.Columns) 
        { 
         if (row[col].ToString() != null) 
         { 
          worksheet.Cell(r, c).Value = colValue; 
          worksheet.Column(c).Width = 10; 
         } 
         c++; 
        } 
        r++; 
       } 

       // change the sheet view to show it in page layout mode 
       worksheet.View.PageLayoutView = false; 

       // save our new workbook and we are done! 
       xlPackage.Save(); 
       xlPackage.Dispose(); 
      } 
     } 
0

访问的Open XML/SpreadsheetML中的文件是远离平凡的工作。该规范庞大而复杂。 “Open XML SDK”(谷歌它)肯定有帮助,但仍然需要一些Open XML标准的知识才能完成。

有一个类似于Excel的API,可以读取和写入Excel Open XML(xlsx)文档以及Excel 97-2003(xls)文档。

您可以看到一些SpreadsheetGear示例here并下载免费试用here

声明:我自己的SpreadsheetGear LLC