2012-12-27 4 views
0

정말 열심히 노력했지만 Excel 시트는 내 기대대로 채워지지 않았습니다. 내용에 문자열 데이터 유형이있는 경우 시트가 그 대신 '0'으로 표시됩니다. 어떤 사람이 나를 도울 수 있다면 아래 코드를 붙여 넣습니다.Open XML SDK 및 LINQ를 사용하여 Excel에 텍스트 삽입

public static void WriteExcelDocument(string FilePath) 
    { 
     try 
     { 
      using (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Open(FilePath, true)) 
      { 
       WorkbookPart workbookPart = spreadSheet.WorkbookPart; 
       IEnumerable<Sheet> Sheets = spreadSheet.WorkbookPart.Workbook.GetFirstChild<Sheets>().Elements<Sheet>().Where(s => s.Name == "data"); 
       if (Sheets.Count() == 0) 
       { 
        // The specified worksheet does not exist. 
        return; 
       } 
       string relationshipId = Sheets.First().Id.Value; 
       WorksheetPart worksheetPart = (WorksheetPart)spreadSheet.WorkbookPart.GetPartById(relationshipId); 
       SheetData sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>(); 
       int index = 2; 
       SpectrumNewEntities context = new SpectrumNewEntities(); 
       var q = from result in context.Appraisers 
         select result; 
       foreach (var g in q) 
       { 
        string Name = g.AppraiserName!=null?g.AppraiserName:String.Empty; 
        string city = g.City != null ? g.City : String.Empty; 
        string Address = g.Address != null ? g.Address : "NA"; 
        int AppId = g.AppraiserAppraiserCompanyId != null ? (int)g.AppraiserAppraiserCompanyId : 0; 
        string email = g.Email != null ? g.Email : String.Empty; 
        Row contentRow = CreateContentRow(index, Name, city, Address, AppId,email); 
        index++; 
        sheetData.AppendChild(contentRow); 
       } 
       // Save the worksheet. 
       worksheetPart.Worksheet.Save(); 
      } 
     } 
     catch (Exception) 
     { 

      throw; 
     } 
    } 

    private static Row CreateContentRow(int index, string Name, string city, string Address, int AppId, string email) 
    { 
     try 
     { 
      //Create new row 
      Row r = new Row(); 
      r.RowIndex = (UInt32)index; 

      //First cell is a text cell, so create it and append it 
      Cell firstCell = CreateTextCell(headerColumns[0], Name, index); 
      r.AppendChild(firstCell);// 

      //create cells that contain data 
      for (int i = 1; i < headerColumns.Length; i++) 
      { 
       Cell c = new Cell(); 
       c.CellReference = headerColumns[i] + index; 
       CellValue v = new CellValue(); 
       if (i == 1) 
       { 
        v.Text = city.ToString(); 
       } 
       if (i == 2) 
       { 
        v.Text = Address.ToString(); 
       } 
       if (i == 3) 
       { 
        v.Text =AppId.ToString(); 
       } 
       if (i == 4) 
       { 
        v.Text = email.ToString(); 
       } 
       c.AppendChild(v); 
       r.AppendChild(c); 

      } 
      return r; 
     } 
     catch (Exception) 
     { 

      throw; 
     } 
    } 
    private static Cell CreateTextCell(string header, string Name,int index) 
    { 
     try 
     { 
      //Create a new inline string cell 
      Cell c = new Cell(); 
      c.DataType = CellValues.InlineString; 
      c.CellReference = header + index; 

      //Add text to text cell 
      InlineString inlineString = new InlineString(); 
      Text t = new Text(); 
      t.Text = Name; 
      inlineString.AppendChild(t); 
      c.AppendChild(inlineString); 
      return c; 
     } 
     catch (Exception) 
     { 

      throw; 
     } 
    } 

왜 디스플레이가 이런 식으로 표시되는지 모르겠습니까? enter image description here

+0

'AppId'를 제외한 모든 셀에'CreateTextCell'을 사용할 수 없습니까? – SearchAndResQ

+0

와우, 실제로 그것은 실수였습니다. 주석 처리 된 줄에서도 첫 번째 셀은 텍스트 셀이므로 추가하기 때문에 무시했습니다. 동의 할 수 있도록 답변을 추가하십시오. –

답변

0

CreateTextCell 메서드를 사용하면 모든 텍스트 셀을 행에 추가 할 수 있습니다. 너는 Name 필드에서하고있는 것처럼.

관련 문제