有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何使用JavaPOI将列的数据从输入excel写入输出excel

我的要求是从输入的excel文件中读取数据,并将其写入输出的excel文件。我使用的是Java8和ApachePOI

在写入过程中,我将特定列的日期格式从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) 个答案