2015-12-03 53 views
1

我有一個要求將數據表導出爲ex​​cel文件。我使用DocumentFormat.OpenXml完成了它。但我不能在導出的Excel中獲取日期(年 - 月 - 日)過濾器,因爲我的單元格被視爲字符串,而不是Excel日期單元格。我使用interop.dll來獲取它,但這非常耗時。我有超過30000條記錄。任何幫助表示讚賞。我的代碼如下: -在C#中使用OpenXml在Excel中創建日期單元格中的問題

protected void Page_Load(object sender, EventArgs e) 
    { 
     DataSet ds=new DataSet(); 
     ds.Tables.Add(GetData()); 
     GenerateExcel(ds); 
    } 

public void GenerateExcel(DataSet data) 
    { 
     var Memstream = new MemoryStream(); 
     using (var workbook = SpreadsheetDocument.Create(Memstream, DocumentFormat.OpenXml.SpreadsheetDocumentType.Workbook)) 
     { 
      var workbookPart = workbook.AddWorkbookPart(); 
      workbook.WorkbookPart.Workbook = new DocumentFormat.OpenXml.Spreadsheet.Workbook(); 
      workbook.WorkbookPart.Workbook.Sheets = new DocumentFormat.OpenXml.Spreadsheet.Sheets(); 
      WorkbookStylesPart wbsp = workbookPart.AddNewPart<WorkbookStylesPart>(); 
      wbsp.Stylesheet = CreateStylesheet1(); 
      wbsp.Stylesheet.Save(); 

      uint sheetId = 0; 

      foreach (System.Data.DataTable table in data.Tables) 
      { 
       var sheetPart = workbook.WorkbookPart.AddNewPart<WorksheetPart>(); 
       var sheetData = new DocumentFormat.OpenXml.Spreadsheet.SheetData(); 
       sheetPart.Worksheet = new DocumentFormat.OpenXml.Spreadsheet.Worksheet(sheetData); 
       DocumentFormat.OpenXml.Spreadsheet.Sheets sheets = workbook.WorkbookPart.Workbook.GetFirstChild<DocumentFormat.OpenXml.Spreadsheet.Sheets>(); 
       string relationshipId = workbook.WorkbookPart.GetIdOfPart(sheetPart); 
       int k = sheets.ChildElements.Count; 
       string bsd = sheets.InnerText; 
       string jknkj = sheets.InnerXml; 
       sheetId = sheetId++; 
       DocumentFormat.OpenXml.Spreadsheet.Sheet sheet = new DocumentFormat.OpenXml.Spreadsheet.Sheet() { Id = relationshipId, SheetId = sheetId, Name = table.TableName }; 
       sheets.Append(sheet); 

       DocumentFormat.OpenXml.Spreadsheet.Row headerRow = new DocumentFormat.OpenXml.Spreadsheet.Row(); 

       List<String> columns = new List<string>(); 
       foreach (DataColumn column in table.Columns) 
       { 
        columns.Add(column.ColumnName); 

         DocumentFormat.OpenXml.Spreadsheet.Cell cell = new DocumentFormat.OpenXml.Spreadsheet.Cell(); 
         cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.String; 
         cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(column.ColumnName); 
         headerRow.AppendChild(cell); 
       } 

       sheetData.AppendChild(headerRow); 

       foreach (DataRow dsrow in table.Rows) 
       { 
        DocumentFormat.OpenXml.Spreadsheet.Row newRow = new DocumentFormat.OpenXml.Spreadsheet.Row(); 
        foreach (String col in columns) 
        { 
         DateTime dDate; 
         if (DateTime.TryParse(dsrow[col].ToString(), out dDate)) 
         { 
          DocumentFormat.OpenXml.Spreadsheet.Cell cell = new DocumentFormat.OpenXml.Spreadsheet.Cell(); 
          cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.Number; 
          cell.StyleIndex = 164; 
          cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(dDate.ToOADate().ToString()); 
          newRow.AppendChild(cell); 
         } 
         else 
         { 
          DocumentFormat.OpenXml.Spreadsheet.Cell cell = new DocumentFormat.OpenXml.Spreadsheet.Cell(); 
          cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.String; 
          cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(dsrow[col].ToString()); 
          newRow.AppendChild(cell); 
         } 
        } 
        sheetData.AppendChild(newRow); 
       } 
      } 
      workbookPart.Workbook.Save(); 
      workbook.Close(); 
     } 
     string [email protected]"D:\\Excel Samples\file_" + DateTime.Now.ToString("MM-dd-yyyyHH-mm-ss") + ".xlsx"; 
     FileStream file = new FileStream(filename, FileMode.Create, FileAccess.Write); 
     Memstream.WriteTo(file); 
     file.Close(); 
     Memstream.Close(); 
    } 
private Stylesheet CreateStylesheet1() 
    { 
     Stylesheet ss = new Stylesheet(); 

     Fonts fts = new Fonts(); 
     DocumentFormat.OpenXml.Spreadsheet.Font ft = new DocumentFormat.OpenXml.Spreadsheet.Font(); 
     FontName ftn = new FontName(); 
     ftn.Val = "Calibri"; 
     DocumentFormat.OpenXml.Spreadsheet.FontSize ftsz = new DocumentFormat.OpenXml.Spreadsheet.FontSize(); 
     ftsz.Val = 11; 
     ft.FontName = ftn; 
     ft.FontSize = ftsz; 
     fts.Append(ft); 
     fts.Count = (uint)fts.ChildElements.Count; 

     Fills fills = new Fills(); 
     Fill fill; 
     PatternFill patternFill; 
     fill = new Fill(); 
     patternFill = new PatternFill(); 
     patternFill.PatternType = PatternValues.None; 
     fill.PatternFill = patternFill; 
     fills.Append(fill); 
     fill = new Fill(); 
     patternFill = new PatternFill(); 
     patternFill.PatternType = PatternValues.Gray125; 
     fill.PatternFill = patternFill; 
     fills.Append(fill); 
     fills.Count = (uint)fills.ChildElements.Count; 

     Borders borders = new Borders(); 
     Border border = new Border(); 
     border.LeftBorder = new LeftBorder(); 
     border.RightBorder = new RightBorder(); 
     border.TopBorder = new TopBorder(); 
     border.BottomBorder = new BottomBorder(); 
     border.DiagonalBorder = new DiagonalBorder(); 
     borders.Append(border); 
     borders.Count = (uint)borders.ChildElements.Count; 

     CellStyleFormats csfs = new CellStyleFormats(); 
     CellFormat cf = new CellFormat(); 
     cf.NumberFormatId = 0; 
     cf.FontId = 0; 
     cf.FillId = 0; 
     cf.BorderId = 0; 
     csfs.Append(cf); 
     csfs.Count = (uint)csfs.ChildElements.Count; 

     uint iExcelIndex = 164; 
     NumberingFormats nfs = new NumberingFormats(); 
     CellFormats cfs = new CellFormats(); 

     cf = new CellFormat(); 
     cf.NumberFormatId = 0; 
     cf.FontId = 0; 
     cf.FillId = 0; 
     cf.BorderId = 0; 
     cf.FormatId = 0; 
     cfs.Append(cf); 

     NumberingFormat nf; 
     nf = new NumberingFormat(); 
     nf.NumberFormatId = iExcelIndex++; 
     nf.FormatCode = StringValue.FromString("mm/dd/yyyy hh:mm:ss"); 
     //nf.FormatCode = StringValue.FromString("mm-d-yy h:mm:ss AM/PM"); 
     nfs.Append(nf); 
     cf = new CellFormat(); 
     cf.ApplyNumberFormat = true; 
     cf.NumberFormatId = nf.NumberFormatId; 
     cf.FontId = 0; 
     cf.FillId = 0; 
     cf.BorderId = 0; 
     cf.FormatId = 0; 
     cfs.Append(cf); 

     nf = new NumberingFormat(); 
     nf.NumberFormatId = iExcelIndex++; 
     nf.FormatCode = "#,##0.0000"; 
     nfs.Append(nf); 
     cf = new CellFormat(); 
     cf.NumberFormatId = nf.NumberFormatId; 
     cf.FontId = 0; 
     cf.FillId = 0; 
     cf.BorderId = 0; 
     cf.FormatId = 0; 
     cf.ApplyNumberFormat = true; 
     cfs.Append(cf); 

     // #,##0.00 is also Excel style index 4 
     nf = new NumberingFormat(); 
     nf.NumberFormatId = iExcelIndex++; 
     nf.FormatCode = "#,##0.00"; 
     nfs.Append(nf); 
     cf = new CellFormat(); 
     cf.NumberFormatId = nf.NumberFormatId; 
     cf.FontId = 0; 
     cf.FillId = 0; 
     cf.BorderId = 0; 
     cf.FormatId = 0; 
     cf.ApplyNumberFormat = true; 
     cfs.Append(cf); 

     // @ is also Excel style index 49 
     nf = new NumberingFormat(); 
     nf.NumberFormatId = iExcelIndex++; 
     nf.FormatCode = "@"; 
     nfs.Append(nf); 
     cf = new CellFormat(); 
     cf.NumberFormatId = nf.NumberFormatId; 
     cf.FontId = 0; 
     cf.FillId = 0; 
     cf.BorderId = 0; 
     cf.FormatId = 0; 
     cf.ApplyNumberFormat = true; 
     cfs.Append(cf); 

     nfs.Count = (uint)nfs.ChildElements.Count; 
     cfs.Count = (uint)cfs.ChildElements.Count; 

     ss.Append(nfs); 
     ss.Append(fts); 
     ss.Append(fills); 
     ss.Append(borders); 
     ss.Append(csfs); 
     ss.Append(cfs); 

     CellStyles css = new CellStyles(); 
     CellStyle cs = new CellStyle(); 
     cs.Name = "Normal"; 
     cs.FormatId = 0; 
     cs.BuiltinId = 0; 
     css.Append(cs); 
     css.Count = (uint)css.ChildElements.Count; 
     ss.Append(css); 

     DifferentialFormats dfs = new DifferentialFormats(); 
     dfs.Count = 0; 
     ss.Append(dfs); 

     TableStyles tss = new TableStyles(); 
     tss.Count = 0; 
     tss.DefaultTableStyle = "TableStyleMedium9"; 
     tss.DefaultPivotStyle = "PivotStyleLight16"; 
     ss.Append(tss); 

     return ss; 
    } 
+0

解決這個問題的一個好方法是在Open XML SDK生產力工具中打開互操作代碼的結果。這可以向您展示1)底層Open XML和2)用於生成工作簿的Open XML SDK代碼。爲此,最好生成一個只有幾個正確的「日期」的工作簿,這樣你就不必通過大量的材料來查找它... –

回答

1

感謝您的回覆。我得到了答案。通過設置StyleIndex = 1而不是164,我得到了解決方案。希望這有助於任何人..

+0

你也有一個錯誤在行'sheetId = sheetId ++;' - 這不會做你可能期望的[這個答案](http://stackoverflow.com/a/4287920/3791802)中描述的 - 你可能只是想'sheetId ++;' – petelids

相關問題