2010-07-19 3 views
0

저는 C# 2008 (FW 3.5), OpenXML SDK 2.0 및 Excel 2007 문서 작업을하고 있습니다.스타일이있는 기존 Excel 문서에 행 추가

프로그램은 데이터베이스에서 값을 가져 와서 동적 테이블을 만들고 그 값을 Excel 템플릿에 붙여 넣습니다. 이 모든 작업의 ​​미세

,하지만 난 다른 뭔가가 필요합니다

나는 템플릿에 새 행의 지정된 번호를 생성해야하지만 (등 테두리, 폰트, 배경색,) 스타일과 난 몰라 그것을 만드는 방법.

누군가이 코드를 작성하는 데 도움이 될 수 있습니까?

고맙습니다. 제 영어를 실례합니다.

답변

0

나는 동일한 상황을 보였지만 더 나은 점은 발견하지 못했다. 그러나 VB 매크로를 사용하여 새로운 행을 추가하고 .net에서 호출하는 Excel 문서를 사용한다.

+0

감사합니다 도움이 희망이

아래에 굵은 두 번 라인 간 화해 요약을 작성하는 것입니다. 서버에서 Office를 설치하지 않기 때문에 매크로를 사용할 수 없습니다. (보안 문제). – Rastro

0

라스트,

당신이해야 할 다음과 같은 방법을 사용할 수 있습니다

private UInt32Value createBorder(Stylesheet styleSheet,bool buttomBorderDouble) 
     { 
      Border border; 
      //set borders of header 
      if (buttomBorderDouble) 
      { 


    border = new Border 
       (
       new BottomBorder { Style = BorderStyleValues.Double }, 
       new DiagonalBorder()); 
     } 
     else 
     { 

      border = new Border 
       (
       new BottomBorder {Style = BorderStyleValues.Thin}, 
       new DiagonalBorder()); 
     } 




     styleSheet.Borders.Append(border); 
     UInt32Value result = styleSheet.Borders.Count; 
     styleSheet.Borders.Count++; 
     return result; 

    } 
    private UInt32Value createFont(Stylesheet styleSheet, string fontName, Nullable<double> fontSize, bool isBold, System.Drawing.Color foreColor, bool isUnderLine) 
    { 

     Font font = new Font(); 

     if (!string.IsNullOrEmpty(fontName)) 
     { 
      FontName name = new FontName() 
      { 
       Val = fontName 
      }; 
      font.Append(name); 
     } 

     if (fontSize.HasValue) 
     { 
      FontSize size = new FontSize() 
      { 
       Val = fontSize.Value 
      }; 
      font.Append(size); 
     } 

     if (isBold == true) 
     { 
      Bold bold = new Bold(); 
      font.Append(bold); 
     } 
     if (isUnderLine == true) 
     { 
      Underline underline = new Underline(); 
      font.Append(underline); 
     } 

     if (foreColor != null) 
     { 
      Color color = new Color() 
      { 
       Rgb = new HexBinaryValue() 
       { 
        Value = 
         System.Drawing.ColorTranslator.ToHtml(
          System.Drawing.Color.FromArgb(
           foreColor.A, 
           foreColor.R, 
           foreColor.G, 
           foreColor.B)).Replace("#", "") 
       } 
      }; 
      font.Append(color); 
     } 
     styleSheet.Fonts.Append(font); 
     UInt32Value result = styleSheet.Fonts.Count; 
     styleSheet.Fonts.Count++; 
     return result; 
    } 

private UInt32Value createCellFormat(Stylesheet styleSheet, UInt32Value fontIndex, UInt32Value fillIndex, UInt32Value numberFormatId, UInt32Value borderId) 
     { 
      CellFormat cellFormat = new CellFormat(); 

      if (fontIndex != null) 
       cellFormat.FontId = fontIndex; 

      if (fillIndex != null) 
       cellFormat.FillId = fillIndex; 

      if (numberFormatId != null) 
      { 
       cellFormat.NumberFormatId = numberFormatId; 
       cellFormat.ApplyNumberFormat = BooleanValue.FromBoolean(true); 
      } 
      if (borderId != null) 
       cellFormat.BorderId = borderId; 

      styleSheet.CellFormats.Append(cellFormat); 

      UInt32Value result = styleSheet.CellFormats.Count; 
      styleSheet.CellFormats.Count++; 
      return result; 
     } 

이러한 방법

스타일 시트 스타일 = workbook.WorkbookStylesPart.Stylesheet 전화 코드의 한 조각이;

UInt32Value headerFontIndex = 
       createFont(
        styleSheet, 
        "MS Sans Seif", 
        10, 
        true, 
        System.Drawing.Color.Black, false); 

UInt32Value doubleBorderIndex = createBorder(styleSheet, true); 

      UInt32Value headerStyleIndexWithDoubleBottomBorder = 
       createCellFormat(
        styleSheet, 
        headerFontIndex, 
        0, 
        null, doubleBorderIndex); 
Cell _Cell = createTextCell(1, 1, "Intercompany Reconciliation Summary", headerStyleIndexWithDoubleButtomBorder, null); 

그 출력은 내가 당신에게 조언을위한

 
Thanks, 
Mohammed Thabet Zaky 
Software Developer 
Cairo,Egypt 
관련 문제