从第一列写完所有行后,如何在Excel中并排打印两列?

时间:2019-06-28 13:38:52

标签: java excel servlets hssfworkbook

我创建了代码,在其中可以通过数据库访问数据,然后将数据写入excel工作表中。

现在数据已成功写入。但是,当将数据写入excel文件第一列的excel工作表中时,问题是

但是在第二列时,这些行打印在第二列中,从第一列的最后一行开始。

我想并列打印列。请参考这张图片link,以获取更好的理解。请说明一下,这将有助于回答我的问题。为了简化起见,我缩短了代码。

    Integer col = 5;
    Double sl_counter = 0.0;
    Double coff_counter = 0.0;
    try {

                ConnectionClass cn = new ConnectionClass();
                Connection con = cn.connectDb();
            String filename = "E:\\export.csv" ;
                HSSFWorkbook workbook = new HSSFWorkbook();
                HSSFSheet sheet = workbook.createSheet("FirstSheet");
            HSSFRow leave_count = sheet.createRow((short)4);
                while(rs7.next()){
                     HSSFRow SL = sheet.createRow(col);
                     SL.createCell(17).setCellValue(rs7.getString("leave_Date"));
                     col++;
                     sl_counter++;
                }
                while(rs11.next()){
                       HSSFRow SL = sheet.createRow(col);
                       SL.createCell(17).setCellValue(rs11.getString("leave_Date")+"      (FIRST HALF DAY)");
                       col++;
                       sl_counter += 0.5;
                }
                while(rs15.next()){
                     HSSFRow SL= sheet.createRow(col);
                     SL.createCell(17).setCellValue(rs15.getString("leave_Date")+"      (SECOND HALF DAY)");
                     col++;
                     sl_counter += 0.5;
                }
                leave_count.createCell(17).setCellValue(sl_counter);

                //int cols = 5;   
                 while(rs8.next()){

                     HSSFRow leave_date = sheet.createRow(col);
                     leave_date.createCell(18).setCellValue(rs8.getString("leave_Date"));
                     col++;
                     coff_counter++;
                }
                while(rs12.next()){
                       HSSFRow leave_date = sheet.createRow(col);
                       leave_date.createCell(18).setCellValue(rs12.getString("leave_Date")+"      (FIRST HALF DAY)");
                       col++;
                       coff_counter += 0.5;
                }
                while(rs16.next()){
                     HSSFRow leave_date = sheet.createRow(col);
                     leave_date.createCell(18).setCellValue(rs16.getString("leave_Date")+"      (SECOND HALF DAY)");
                     col++;
                     coff_counter += 0.5;
                }

                leave_count.createCell(18).setCellValue(coff_counter);

                FileOutputStream fileOut = new FileOutputStream(filename);
                workbook.write(fileOut);
                fileOut.close();
    //            workbook.close();
                cl_counter = 0.0;
                col = 0;

            } catch ( Exception ex ) {
                System.out.println(ex);
            } 

1 个答案:

答案 0 :(得分:0)

为了创建新的工作表,您需要第二个对象 HSSFSheet ,并需要对其进行初始化。

我已将URL中的poi jar添加到Access HSSFSheet类。

我已经使用临时数据列表准备了带有HSSF库的演示,因此您可以对如何使用该库通过创建多个工作表来生成excel文件有一个小小的想法。

import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class Excel {
    public static void main(String[] args) {
        int col = 1;
        try {

            String filename = "I:\\export.csv";
            HSSFWorkbook workbook = new HSSFWorkbook();
            HSSFSheet sheet = workbook.createSheet("FirstSheet");
            List rs7 = new ArrayList();
            rs7.add("06/27/2019");

            List rs8 = new ArrayList();
            rs8.add("06/23/2019");

            List rs11 = new ArrayList();
            rs11.add("06/23/2019");

            List rs12 = new ArrayList();
            rs12.add("06/26/2019");

            List rs15 = new ArrayList();
            rs15.add("06/26/2019");

            List rs16 = new ArrayList();
            rs16.add("06/25/2019");

            int r7,r8, r11,r12, r15,r16;
            r7=r8=r11=r12=r15=r16=0;
            sheet.createRow(0).createCell(1).setCellValue("SL");
            while (r7<rs7.size()) {
                HSSFRow SL = sheet.createRow(col++);
                SL.createCell(1).setCellValue(""+rs7.get(r7++));
            }
            while (r11<rs11.size()) {
                HSSFRow SL = sheet.createRow(col++);
                SL.createCell(1).setCellValue(rs11.get(r11++) + "      (FIRST HALF DAY)");
            }
            while (r15<rs15.size()) {
                HSSFRow SL = sheet.createRow(col++);
                SL.createCell(1).setCellValue(rs15.get(r15++) + "      (SECOND HALF DAY)");
            }


            HSSFSheet sheet2 = workbook.createSheet("SecondSheet");
            sheet2.createRow(0).createCell(1).setCellValue("C-OFF");
            col=1;
            while (r8<rs8.size()) {

                HSSFRow leave_date = sheet2.createRow(col++);
                leave_date.createCell(1).setCellValue(""+rs8.get(r8++));
            }
            while (r12<rs12.size()) {
                HSSFRow leave_date = sheet2.createRow(col++);
                leave_date.createCell(1).setCellValue(rs12.get(r12++) + "      (FIRST HALF DAY)");
            }
            while (r16<rs16.size()) {
                HSSFRow leave_date = sheet2.createRow(col++);
                leave_date.createCell(1).setCellValue(rs16.get(r16++) + "      (SECOND HALF DAY)");
            }
            FileOutputStream fileOut = new FileOutputStream(filename);
            workbook.write(fileOut);
            fileOut.close();
            col = 0;

        } catch (Exception ex) {
            System.out.println(ex);
        }
    }
}

如果您不需要创建多个工作表并且只需要在一个数据表中显示数据,则可以用下面的代码替换上面的第二部分,以帮助您了解用法。

//HSSFSheet sheet2 = workbook.createSheet("SecondSheet");
            sheet.getRow(0).createCell(2).setCellValue("C-OFF");
//          sheet2.createRow(0).createCell(1).setCellValue("C-OFF");
            col=1;
            while (r8<rs8.size()) {
                sheet.getRow(col++).createCell(2).setCellValue(""+rs8.get(r8++));
                /*HSSFRow leave_date = sheet2.createRow(col++);
                leave_date.createCell(1).setCellValue(""+rs8.get(r8++));*/
            }
            while (r12<rs12.size()) {
                sheet.getRow(col++).createCell(2).setCellValue(""+rs12.get(r12++)+ "      (FIRST HALF DAY)");
                /*HSSFRow leave_date = sheet2.createRow(col++);
                leave_date.createCell(1).setCellValue(rs12.get(r12++) + "      (FIRST HALF DAY)");*/
            }
            while (r16<rs16.size()) {
                sheet.getRow(col++).createCell(2).setCellValue(""+rs16.get(r16++)+ "      (SECOND HALF DAY)");
                /*HSSFRow leave_date = sheet2.createRow(col++);
                leave_date.createCell(1).setCellValue(rs16.get(r16++) + "      (SECOND HALF DAY)");*/
            }
            FileOutputStream fileOut = new FileOutputStream(filename);
            workbook.write(fileOut);
            fileOut.close();
            col = 0;