写入下一个空白行Excel - Java

时间:2018-03-13 15:14:18

标签: java excel apache-poi xssf

我的代码将数据写入excel,但它会覆盖相同的行和列。我不想覆盖具有列标题的行以及之后的行(如果其中包含数据)。

步骤 1输入列标题 2以行/列输入数据 3如果列标题不存在列标题 4如果填充的行/列转到下一个 5重复1-4

我的代码如下。任何帮助将不胜感激。

    //Create blank workbook
                      XSSFWorkbook workbook = new XSSFWorkbook();

                      //Create a blank sheet
                      XSSFSheet spreadsheet = workbook.createSheet( " Employee Info ");

                      //Create row object
                      XSSFRow row;

                      //This data needs to be written (Object[])
                      Map < String, Object[] > clientinfo = new TreeMap < String, Object[] >();
                      clientinfo.put( "1", new Object[] {
                         "PayRoll Status", "Week #", "Pay Date","End Date" });

                      clientinfo.put( "2", new Object[] {
                      CycleStatus, WeekID, DateId, EndDateId });


                      //Iterate over data and write to sheet
                      Set < String > keyid = clientinfo.keySet();
                      int rowid = 0;

                      for (String key : keyid) {
                         row = spreadsheet.createRow(rowid++);
                         Object [] objectArr = clientinfo.get(key);
                         int cellid = 0;

                         for (Object obj : objectArr){
                            Cell cell = row.createCell(cellid++);
                            cell.setCellValue((String)obj);
                         }
                      }
                      //Write the workbook in file system
                      FileOutputStream out = new FileOutputStream(
                         new File("C:/PayrollSync/Pre-Payroll.xlsx"));

                      workbook.write(out);
                      out.close();
                      System.out.println("Prepayroll.xlsx written successfully");

这是我提出的解决方案,但它写行的计数是否有任何方法可以将其取消

  String excelFilePath = "C://PayrollSync//Pre-Payroll.xlsx";
                            FileInputStream inputStream = new FileInputStream(new File(excelFilePath));
                            Workbook workbook = WorkbookFactory.create(inputStream);

                            Sheet sheet = workbook.getSheetAt(0);

                            //CycleStatus, WeekID, DateId, EndDateId
                            Object[][] bookData = {
                                    {CycleStatus,WeekID,DateId,EndDateId},

                            };

                            int rowCount = sheet.getLastRowNum();

                            for (Object[] aBook : bookData) {
                                Row row = sheet.createRow(++rowCount);

                                int columnCount = 0;

                                Cell cell = row.createCell(columnCount);
                                cell.setCellValue(rowCount);

                                for (Object field : aBook) {
                                    cell = row.createCell(++columnCount);
                                    if (field instanceof String) {
                                        cell.setCellValue((String) field);
                                    } else if (field instanceof Integer) {
                                        cell.setCellValue((Integer) field);
                                    }
                                }

                            }



                            FileOutputStream outputStream = new FileOutputStream("C://PayrollSync//Pre-Payroll.xlsx");
                            workbook.write(outputStream);
                            inputStream.close();
                            outputStream.close();

1 个答案:

答案 0 :(得分:1)

我会这样做:

import com.google.common.collect.Lists;
import no.tollpost.web.termodash.rest.report.excel.model.ExcelReportDTO;
import no.tollpost.web.termodash.rest.report.excel.model.RowDTO;
import org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;

public final class ExcelGenerator {

  private ExcelGenerator() {
  }

  public static void getExcelVersion(final OutputStream outputStream) {
    try {
        final Workbook book = new XSSFWorkbook();
        generateReport(book);
        book.write(outputStream);
    } catch (final IOException | OpenXML4JRuntimeException e) {
        //log.warn("Failed to build excel report", e);
    }
  }

  private static void generateReport(final Workbook book) {
    final Sheet sheet = book.createSheet("Sheet_name);
    final List<String> columnNames = getColumnNames();
    final List<RowDTO> rows = getRows(getYourData()); // get your data as list of your DTOs

    generateTableHeader(sheet, book, columnNames);
    generateTableRows(sheet, rows);

    IntStream.range(0, columnNames.size()).forEach(sheet::autoSizeColumn);
  }

  private List<YourDTO> getYourData() {
  return // Your data as List (each element will be row in the excel sheet)
  }

  private static List<String> getColumnNames() {

    return Lists.newArrayList("column_name_1", "column_name_2", "column_name_3");
  }

  private static List<RowDTO> getRows(final List<YourDTO> data) {
        final List<RowDTO> rows = new ArrayList<>();
        IntStream.range(0, data.size()).forEach(i -> {
            final List<String> cellsMapper = new ArrayList<>();
            cellsMapper.add(data.get(i).getSomeProperty());
            cellsMapper.add(data.get(i).getOtherProperty());
            cellsMapper.add(data.get(i).getThirdProperty());
            cellsMapper.add(data.get(i).getFourthProperty());

            rows.add(new RowDTO(cellsMapper));
        });

        // rows.sort(Comparator.comparing(row -> row.getData().get(3)));

        return rows;
  }

  private static void generateTableHeader(final Sheet sheet, final Workbook workbook, final List<String> cellTitles) {
    final Row tabellHeader = sheet.createRow(0);

    IntStream.range(0, cellTitles.size()).forEach(cellNummer -> {
        final Cell cell = tabellHeader.createCell(cellNummer);
        cell.setCellValue(cellTitles.get(cellNummer));
        setHeaderCellStyle(cell, workbook, cellTitles.get(cellNummer));
    });
  }

  private static void setHeaderCellStyle(final Cell celle, final Workbook workbook, final String celleTittel) {
    if (!celleTittel.isEmpty()) {
        celle.setCellStyle(getHeaderCellStyle(workbook));
    }
  }

  private static CellStyle getHeaderCellStyle(final Workbook workbook) {
    final CellStyle style = workbook.createCellStyle();
    style.setFillForegroundColor(IndexedColors.PALE_BLUE.getIndex());
    style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    final Font font = workbook.createFont();
    font.setBoldweight(Font.BOLDWEIGHT_BOLD);
    style.setFont(font);

    return style;
  }

  private static void generateTableRows(final Sheet sheet, final List<RowDTO> rows) {
    IntStream.range(0, rows.size()).forEach(rowNUmber -> {
        final Row row = sheet.createRow(rowNUmber + 1);

        IntStream.range(0, rows.get(rowNUmber).getData().size())
                 .forEach(cellNumber -> setCellValue(cellNumber, rowNUmber, row, rows));
    });
  }

  private static void setCellValue(final int cellNumber, final int rowNumber, final Row row, final List<RowDTO> rows) {
    final Cell cell = row.createCell(cellNumber);
    cell.setCellValue(rows.get(rowNumber).getData().get(cellNumber));
    cell.setCellType(Cell.CELL_TYPE_STRING);
  }
}

final class RowDTO {

    private final List<String> data;

    public RowDTO(final List<String> data) {
    this.data = data;
    }

    public List<String> getData() {
       return data;
   }
}