2011-11-27 2 views
0

asp에서 NPOI를 사용하여 전화 번호가있는 열을 내보내는 동안 맨 앞의 0을 유지하려고합니다. net mvc 3 응용 프로그램. 나는 여기서 읽었다; http://creativyst.com/Doc/Articles/CSV/CSV01.htm#CSVAndExcel = "[0으로 시작하는 숫자]"를 추가 할 수 있다는 점에 유의하십시오. 또한 텍스트를 셀로 설정하면 0을 유지할 것이라고 읽었습니다. 나는 이것을 시도한 나의 시도에서 실패했다. 여기 내 코드가있다.NPOI를 사용하여 내보낼 때 NPOI를 사용하여 내보낼 때 "0xxxx"를 추가하여 선행 0을 유지하거나 셀을 텍스트로 설정하는 방법

public ActionResult Export(int page, string orderBy, string filter) 
    { 
     //Get the data representing the current grid state - page, sort and filter 
     GridModel model = Model().ToGridModel(page, 10, orderBy, string.Empty, filter); 
     var orders = model.Data.Cast<Advertiser>(); 

     //Create new Excel workbook 
     var workbook = new HSSFWorkbook(); 


     //Create new Excel sheet 
     var sheet = workbook.CreateSheet(); 


     //(Optional) set the width of the columns 
     sheet.SetColumnWidth(0, 10 * 256); 
     sheet.SetColumnWidth(1, 50 * 256); 
     sheet.SetColumnWidth(2, 50 * 256); 
     sheet.SetColumnWidth(3, 50 * 256); 

     //Create a header row 
     var headerRow = sheet.CreateRow(0); 



     //Set the column names in the header row 
     headerRow.CreateCell(0).SetCellValue("Name"); 
     headerRow.CreateCell(1).SetCellValue("Phone"); 
     headerRow.CreateCell(5).SetCellValue("Company Name"); 
     headerRow.CreateCell(7).SetCellValue("Address 1"); 
     headerRow.CreateCell(8).SetCellValue("Address 2"); 
     headerRow.CreateCell(9).SetCellValue("Address 3"); 
     headerRow.CreateCell(10).SetCellValue("Address 4"); 
     headerRow.CreateCell(11).SetCellValue("Post Code"); 
     headerRow.CreateCell(14).SetCellValue("Email"); 
     headerRow.CreateCell(16).SetCellValue("Website"); 
     headerRow.CreateCell(19).SetCellValue("Listing Type"); 

     //(Optional) freeze the header row so it is not scrolled 
     sheet.CreateFreezePane(0, 1, 0, 1); 

     int rowNumber = 1; 

     //Populate the sheet with values from the grid data 
     foreach (Advertiser order in orders) 
     { 
      //Create a new row 
      var row = sheet.CreateRow(rowNumber++); 

      //Set values for the cells 
      row.CreateCell(0).SetCellValue(order.AdvertiserName); 
      row.CreateCell(1).SetCellValue(order.Phone); 
      row.CreateCell(3).SetCellValue(order.CompanyName); 
      row.CreateCell(5).SetCellValue(order.Address1); 
      row.CreateCell(6).SetCellValue(order.Address2); 
      row.CreateCell(7).SetCellValue(order.Address3); 
      row.CreateCell(8).SetCellValue(order.Address4); 
      row.CreateCell(9).SetCellValue(order.Postcode); 
      row.CreateCell(10).SetCellValue(order.AdvertiserEmail); 
      row.CreateCell(11).SetCellValue(order.Website); 
      row.CreateCell(12).SetCellValue(order.listing.type); 



     } 

     //Write the workbook to a memory stream 
     MemoryStream output = new MemoryStream(); 
     workbook.Write(output); 

     //Return the result to the end user 

     return File(output.ToArray(), //The binary data of the XLS file 
      "application/vnd.ms-excel", //MIME type of Excel files 
      "Advertisers.xls");  //Suggested file name in the "Save as" dialog which will be displayed to the end user 
    } 
} 

행운을 빌어 여러 곳에서 setCellTyoe 메서드를 사용해 보았습니다.

어떻게했는지는 신경 쓰지 않아도 시트를 내보낼 때 앞에 오는 0을 유지하기 만하면됩니다.

답변

0

데이터 형식을 string에서 int로 변경하고 NPOI가 셀을 텍스트로 설정하여 선행 0을 유지합니다.

+0

직접 답변을해도 답변을 수락해야합니다. 다른 사람이 문제가 해결되었음을 알 수 있도록 :) – JimmyPena

+0

고마워, 18 시간이나 기다려야 만 했어. 잊어 버렸어, 나에게 상기시켜 줘서 고마워! – Dan

1

테스트 할 수 없지만 createCell 호출에서 유형을 설정해 보았습니까? 다른 모든 실패하면 당신은 앞에 0이되기 전에 작은 따옴표를 넣어의 노력이 필요한 해킹에 의지 할 수 :

'00185 

이 텍스트로 셀을 강제하고 엑셀은 편집에 표시해야한다.

+0

안녕하세요. Mark, 답장을 보내 주셔서 감사합니다. 나는 세포 유형을 설정하려했지만 실패했다. 그 제안과 '해킹'의 조합은 내 모델의 속성 유형을 int에서 문자열로 변경하여 차이가 있는지 확인할 수 있음을 깨닫게했습니다. 그것은, 지금 npoi는 텍스트로 그 세포를 취급하고 선도 0을 유지합니다. – Dan

관련 문제