1

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

现在数据已成功写入。但问题是当数据被写入excel文件第一列的excel表时,所有数据都正确写入。

但是在第二列时,行打印在第二列中,最后一行从第一列结束。

我想并排打印列。请参阅此图片链接以获得更好的理解。请说明一下,这将有助于回答我的问题。为了简单起见,我缩短了我的代码。

    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);
            } 
4

1 回答 1

0

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

我已将 poi jar 从URL添加到 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;
于 2019-07-02T04:53:38.110 回答