2017-10-27 4 views
0

아래 코드를 사용하여 셀을 엑셀로 지정하지만 Excel 셀에 이중 값이 표시되도록 날짜 값을 설정하고 있습니다. 요구 사항은 열 때 날짜 값을 보는 것입니다.apache로 셀을 엑셀하기위한 날짜 형식 설정 POI가 작동하지 않습니다.

row = sheet.createRow(rowNum++); 
cell = row.createCell(0); 

XSSFCellStyle cellStyle = (XSSFCellStyle)cell.getCellStyle();  
cellStyle.setDataFormat(workbook.createDataFormat().getFormat("mm/dd/yyyy")); 
Date loginDate = formatStringToDate(entry.getKey(),"yyyy-MM-dd"); 
cell.setCellValue(loginDate); 
cell.setCellStyle(cellStyle); 

나는 열고 난 다음 셀에 날짜 값을 보여주고있다 엑셀에서 날짜 형식을 변경하는 경우 enter image description here

아래 그림과 같이 일반으로 표시되어 셀 서식을 볼 수 있지만 내가 원하는 때 표시 할 셀

답변

2

먼저 Excel이 셀 스타일을 관리하는 방법을 알아야합니다. 각 셀에 자체 셀 스타일이 없도록 통합 문서 수준에서이 작업을 수행합니다. 대신 필요에 따라 다른 셀 스타일이 있으며 셀은 이러한 셀 스타일 중 하나를 사용합니다.

새로 만든 셀에 기본 셀 스타일이 있고이 새 셀에 Cell.getCellStyle을 사용하면이 기본 셀 스타일을 얻게됩니다. 따라서 코드는 기본 셀 스타일을 날짜 형식의 셀 스타일로 만듭니다. 이것은 갈 길이 아닙니다.

그래서 처음에는 통합 문서 수준에서 필요한만큼의 셀 스타일을 만듭니다. 예를 들어 날짜 형식의 셀 스타일과 통화 형식의 셀 스타일이 있습니다.

그런 다음 시트와 셀을 만들고 데이터를 셀에 넣습니다. 셀에 특수 셀 스타일이 필요한 경우 이전에 만든 셀 스타일 중 하나를 사용하고 있습니다.

예 :

import java.io.FileOutputStream; 

import org.apache.poi.ss.usermodel.*; 
import org.apache.poi.xssf.usermodel.XSSFWorkbook; 

import java.util.Date; 
import java.util.GregorianCalendar; 
import java.util.Map; 
import java.util.TreeMap; 
import java.util.List; 
import java.util.Arrays; 

public class CreateExcelNumberFormats { 

public static void main(String[] args) throws Exception { 

    Map<Date, List<Object>> data = new TreeMap<Date, List<Object>>(); 

    data.put(new GregorianCalendar(2017, 9, 29, 6, 0).getTime(), Arrays.asList("user 1", 1234.56)); 
    data.put(new GregorianCalendar(2017, 9, 30, 6, 0).getTime(), Arrays.asList("user 2", 789.12)); 
    data.put(new GregorianCalendar(2017, 9, 31, 6, 0).getTime(), Arrays.asList("user 3", 131415.16)); 
    data.put(new GregorianCalendar(2017, 9, 29, 15, 45).getTime(), Arrays.asList("user 4", 1234567.89)); 
    data.put(new GregorianCalendar(2017, 9, 30, 9, 45).getTime(), Arrays.asList("user 5", 123.45)); 

    Workbook wb = new XSSFWorkbook(); 
    CreationHelper creationHelper = wb.getCreationHelper(); 

    //on workbook level we are creating as much cell styles as needed: 
    CellStyle datestyle = wb.createCellStyle(); 
    datestyle.setDataFormat(creationHelper.createDataFormat().getFormat("mm/dd/yyyy")); 
    CellStyle currencystyle = wb.createCellStyle(); 
    currencystyle.setDataFormat(creationHelper.createDataFormat().getFormat("$#,##0.00")); 

    //now we are creating the sheet and the cells and are putting the data into the cells 
    Sheet sheet = wb.createSheet(); 
    Row row = sheet.createRow(0); 
    Cell cell = row.createCell(0); 
    cell.setCellValue("Date"); 
    cell = row.createCell(1); 
    cell.setCellValue("Logged in User"); 
    cell = row.createCell(2); 
    cell.setCellValue("Amount"); 

    int rowNum = 1; 

    for (Map.Entry<Date, List<Object>> entry : data.entrySet()) { 

    row = sheet.createRow(rowNum++); 
    cell = row.createCell(0); 
    Date loginDate = entry.getKey(); 
    cell.setCellValue(loginDate); 
    //if the cell needs a special cell style, then we are using one of the ones we have previous created 
    cell.setCellStyle(datestyle); 

    List<Object> userdatas = entry.getValue(); 

    int cellNum = 1; 
    for (Object userdata : userdatas) { 
    cell = row.createCell(cellNum); 
    if (cellNum == 1) { 
    cell.setCellValue((String)userdata); 
    } else if (cellNum == 2) { 
    cell.setCellValue((Double)userdata); 
    //if the cell needs a special cell style, then we are using one of the ones we have previous created 
    cell.setCellStyle(currencystyle); 
    } 
    cellNum++; 
    } 

    } 

    wb.write(new FileOutputStream("CreateExcelNumberFormats.xlsx")); 
    wb.close(); 

} 

} 
+0

신난다. 그것은 효과가 있었다. 자세한 설명 주셔서 감사합니다. – Mohan