在 excel sheet 中生成报告 java

generating report in excel sheet in java

我想生成 Excel 报告,但无法生成 excel 报告,我不知道是什么问题?

每次单击生成报告按钮时,我都需要生成自动报告。 我正在使用 sqlyog,我的 table 名称是最终名称,我的数据库名称是等等。我的数据库 table 条目不是静态的,所以我需要一个自动报告。

我正在使用 Eclipse IDE 是不是我需要再使用外部 api.

import java.io.File;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class ExcelDatabase {
public static void main(String[] args) throws Exception {
    Class.forName("com.mysql.jdbc.Driver");
    Connection connect = DriverManager.getConnection("jdbc:mysql://localhost/etc", "root", "");

    Statement statement = connect.createStatement();
    ResultSet resultSet = statement.executeQuery("select * from final");
    HSSFWorkbook workbook = new HSSFWorkbook();
    HSSFSheet spreadsheet = workbook.createSheet("engine report");
    HSSFRow row = spreadsheet.createRow(1);
    HSSFCell cell;
    cell = row.createCell(1);
    cell.setCellValue("engine_code");
    cell = row.createCell(2);
    cell.setCellValue("var1");
    cell = row.createCell(3);
    cell.setCellValue("var2");
    cell = row.createCell(4);
    cell.setCellValue("var3");
    cell = row.createCell(5);
    cell.setCellValue("var4");
    cell = row.createCell(6);
    cell.setCellValue("var5");
    cell = row.createCell(7);
    cell.setCellValue("User_Name");
    cell = row.createCell(8);
    cell.setCellValue("time_stamp");
    int i = 2;
    while (resultSet.next()) {
        row = spreadsheet.createRow(i);
        cell = row.createCell(1);
        cell.setCellValue(resultSet.getInt("ec"));
        cell = row.createCell(2);
        cell.setCellValue(resultSet.getString("v1"));
        cell = row.createCell(3);
        cell.setCellValue(resultSet.getString("v2"));
        cell = row.createCell(4);
        cell.setCellValue(resultSet.getString("v3"));
        cell = row.createCell(5);
        cell.setCellValue(resultSet.getString("v4"));
        cell = row.createCell(6);
        cell.setCellValue(resultSet.getString("v5"));
        cell = row.createCell(7);
        cell.setCellValue(resultSet.getString("user"));
        cell = row.createCell(8);
        cell.setCellValue(resultSet.getString("time"));
        i++;
    }
    FileOutputStream out = new FileOutputStream(new File("exceldatabase.xls"));
    workbook.write(out);
    out.close();
    System.out.println("exceldatabase.xls written successfully");
 }
}

您可以使用 Apache poi 库 类 通过 excel sheet 代码创建 excel sheet,例如 HSSFSheet、HSSFRow。

import java.io.ByteArrayOutputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;

public class CreateExcel(){
    public static void main(String args[]){
       ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
       HSSFWorkbook workbook = new HSSFWorkbook();
       HSSFSheet sheet = workbook.createSheet("sheet 1");
       HSSFRow row = sheet.createRow(rowNumber); // 0,1,2..
       HSSFCell cell = row.createCell(columnNumber); // 0,1,2...
       cell.setCellValue("Hello Apache POI !");
       HSSFFont font = workbook.createFont();
       HSSFCellStyle style = workbook.createCellStyle();
       style.setFont(font);
       cell.setCellStyle(style);
       workbook.write(baos);
       baos.flush();
  }
}

使用上面的程序你可以创建一个 excel sheet.

我在数据库中创建了与您相同的 table 并尝试了 运行 您的代码。 我可以在不更改代码的情况下创建 Excel 文件。 不同之处在于我使用了不同的 driver ("oracle.jdbc.driver.OracleDriver")。所以首先检查你的数据库连接。如果成功,则其余代码应该可以正常工作。
请 post 更具体的例外情况(如果有)。 这将有助于解决问题。 还有一件事您使用了第 1 行和第 1 单元格的索引,但 POI 使用从 0 开始的行和列的索引。

读取Excel文件并生成如下报告

您可以从 excel 中读取所有行和列并将其显示在您的 UI 中。

    FileInputStream file = new FileInputStream("exceldatabase.xls");
    Workbook wb = new HSSFWorkbook(file);
    Sheet sheet = wb.getSheet("engine report");
    int lastRowNum = sheet.getLastRowNum();
    for(int rowIndex = 0 ; rowIndex < lastRowNum ; rowIndex++){
        Row currRow = sheet.getRow(rowIndex);
        if(currRow != null) {
            List<String> currRowValues = new ArrayList<String>();
            for(int cellNo = currRow.getFirstCellNum(); cellNo < currRow.getLastCellNum();cellNo++) {
                Cell currCell = currRow.getCell(cellNo);

                if(currCell != null) {
                    int cellType = currCell.getCellType();
                    switch(cellType) {
                        case Cell.CELL_TYPE_BLANK :
                            currRowValues.add("");
                        break;
                        case Cell.CELL_TYPE_BOOLEAN :
                            currRowValues.add(String.valueOf(currCell.getBooleanCellValue()));
                        break;
                        case Cell.CELL_TYPE_NUMERIC :
                            currRowValues.add(String.valueOf(currCell.getNumericCellValue()));
                        break;
                        case Cell.CELL_TYPE_STRING :
                            currRowValues.add(currCell.getStringCellValue());
                        break;
                        case Cell.CELL_TYPE_ERROR :
                            currRowValues.add("");
                        break;

                    }
                } else {
                    currRowValues.add("");
                }

            }

            // Add your code here 
            // Add current list to your UI or the way you want to display report
                System.out.println( currRowValues);
        }
    } 

要在 Excel 文件中添加 Header,请使用以下代码。

您应该在 sheet 中创建一个合并区域。

您可以使用 CellRangeAddress 提供要合并的范围。它以 startRow、endRow、startCol、endCol 作为值来创建单元格范围地址。

创建合并区域后,您应该在区域中最左侧的单元格中设置值,即 startRow、startCol 处的单元格。

我使用对齐方式将内容居中对齐。

保存你的文件,你会得到预期的结果。 :)

    HSSFRow createRow = spreadsheet.createRow(0);
    CellRangeAddress range = new CellRangeAddress(0, 0, 1, 8);
    spreadsheet.addMergedRegion(range);
    HSSFCell createCell = createRow.createCell(1);
    createCell.setCellValue("My header");//ADD Your Custom Header value Here 
    CellUtil.setAlignment(createCell, workbook, CellStyle.ALIGN_CENTER);

您似乎在一次又一次地覆盖相同的行和单元格对象。 对于每个新单元格,您需要创建一个新对象:

               //instead of 
               HSSFCell cell;
               cell = row.createCell(1);
               cell.setCellValue("engine_code");
               cell = row.createCell(2);
               cell.setCellValue(resultSet.getString("v1"));

               //do
               HSSFCell cell1 = row.createCell(1);
               cell1.setCellValue("engine_code");
               HSSFCell cell2 = row.createCell(2);
               cell2.setCellValue(resultSet.getString("v1"));

这同样适用于行对象:

               HSSFRow row1 = spreadsheet.createRow(1);
               HSSFRow row2 = spreadsheet.createRow(2);