2012-07-19 48 views

回答

2

我得到了答案,並與你分享一切...

只需添加單元格,因爲我們做..

Cell cell =new Cell(){ cellReference=A1 }; //Or other necessary details 
cell.cellValue = new CellValue(DateTime.Now.ToOADate().ToString()); 

cell.StyleIndex=5; 

在這裏,我已經使用

cell.StyleIndex=5; 

這是默認Excel中的日期樣式索引。因此,沒有必要添加所有的外部styylesheets

享受:)

+0

甚至沒有一個函數DateTime.Now.ToDouble – user236215 2012-11-17 06:47:04

+0

我編輯了答案..現在嘗試 – 2012-11-19 05:24:36

1

所以沒有必要添加所有的外部樣式表

我無法得到它沒有樣式表的工作。

我用This Blog post得到它的工作。除了需要使用日期的CellStyleFormat和CellFormat部分外,樣式表還需要Font,Border,Fill,DifferentialFormat和TableStyle部分。

private static Stylesheet CreateStylesheet() 
    { 
     Stylesheet ss = new Stylesheet(); 

     Fonts fonts = new Fonts(new OpenXmlElement[] 
     { 
      new Font 
      { 
       FontName = new FontName { Val = "Calibri" }, 
       FontSize = new FontSize { Val = 11 } 
      } 
     }); 
     fonts.Count = (uint)fonts.ChildElements.Count; 

     Fills fills = new Fills(new OpenXmlElement[] 
     { 
      new Fill 
      { 
       PatternFill = new PatternFill { PatternType = PatternValues.None } 
      } 
     }); 
     fills.Count = (uint)fills.ChildElements.Count; 

     Borders borders = new Borders(new OpenXmlElement[] 
     { 
      new Border 
      { 
       LeftBorder = new LeftBorder(), 
       RightBorder = new RightBorder(), 
       TopBorder = new TopBorder(), 
       BottomBorder = new BottomBorder(), 
       DiagonalBorder = new DiagonalBorder(), 
      } 
     }); 
     borders.Count = (uint)borders.ChildElements.Count; 

     CellStyleFormats csfs = new CellStyleFormats(new OpenXmlElement[] 
     { 
      new CellFormat 
      { 
       NumberFormatId = 0, 
       FontId = 0, 
       FillId = 0, 
       BorderId = 0, 
      } 
     }); 
     csfs.Count = (uint)csfs.ChildElements.Count; 

     CellFormats cfs = new CellFormats(new OpenXmlElement[] 
     { 
      new CellFormat 
      { 
       NumberFormatId = 0, 
       FontId = 0, 
       FillId = 0, 
       BorderId = 0, 
       FormatId = 0, 
      }, 
      new CellFormat 
      { 
       NumberFormatId = 14, 
       FontId = 0, 
       FillId = 0, 
       BorderId = 0, 
       FormatId = 0, 
       ApplyNumberFormat = true 
      } 
     }); 

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

     ss.Append(fonts); 
     ss.Append(fills); 
     ss.Append(borders); 
     ss.Append(csfs); 
     ss.Append(cfs); 

     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; 
    } 

一旦樣式表就位,您可以設置StyleIndex。

cell.StyleIndex=14 
相關問題