如何使用JAVA POI从输入excel输出列的数据以输出excel

时间:2018-04-25 12:37:31

标签: java excel apache-poi

我的要求是从输入excel文件中读取数据并将其写入输出excel文件中我正在使用JAVA 8和APACHE POI

在写入过程中,我将特定列的日期格式从 mm / dd / yyyy 更改为 dd / mm / yyyy

使用以下代码将其设置为excel中的当前日期,我想在输出excel中设置相同的输入excel值,但定义了新格式。

任何帮助都会非常感激

以下是更新代码:

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.util.Iterator;
    import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
    import org.apache.poi.openxml4j.opc.OPCPackage;
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.CellStyle;
    import org.apache.poi.ss.usermodel.CreationHelper;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.ss.usermodel.Sheet;
    import org.apache.poi.ss.usermodel.Workbook;
    import org.apache.poi.ss.usermodel.WorkbookFactory;
    import org.apache.poi.xssf.usermodel.XSSFCellStyle;
    import org.apache.poi.xssf.usermodel.XSSFCreationHelper;
    import org.apache.poi.xssf.usermodel.XSSFRow;
    import org.apache.poi.xssf.usermodel.XSSFSheet;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;


  class DateFormatDemo1{ 
    public static void main( String[] args ) throws IOException, InvalidFormatException, ParseException
    {
        OPCPackage pkg = OPCPackage.open(new File("C:\\DateFormatIssue\\SourceFile\\S.xlsx"));
        XSSFWorkbook wb = new XSSFWorkbook(pkg); 
        XSSFCreationHelper createHelper = wb.getCreationHelper();

        XSSFSheet sheet = wb.getSheetAt(0);      // Create spreadsheet in workbook
        Iterator<Row> iterator = sheet.iterator();
        Cell cell = null;
        Row row=null;
        row=iterator.next();
        int pos=11;
        while(iterator.hasNext())
        {
            row=iterator.next();

            cell= row.getCell(pos-1);


            XSSFCellStyle cellStyle = (XSSFCellStyle)cell.getCellStyle();
            cellStyle.setDataFormat( createHelper.createDataFormat().getFormat("M/dd/yyyy")); // set the format of the date  
            SimpleDateFormat sdf = new SimpleDateFormat("d/M/yyyy");           
            Date d=null;


            /** below if block converts dates which are in format d/M/yyyy (15/04/2017) to M/d/yyyy (4/15/2017)**/
            if (cell.getCellType() == Cell.CELL_TYPE_STRING)
            {    

                d= sdf.parse(cell.getStringCellValue());
                cell.setCellValue(d);
                cell.setCellStyle(cellStyle);       

            }


            else  if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC)
            {
                cell.setCellValue(cell.getNumericCellValue()); 

            }
        }




        FileOutputStream outFile =new FileOutputStream(new File("C:\\DateFormatIssue\\OutputFile\\output.xlsx"));
        wb.write(outFile);
        wb.close();
        outFile.close();
        pkg.close();




    }}

0 个答案:

没有答案