在java中的excel表格中生成报告

我想生成Excel报表,但是我不能生成Excel报表,我不知道是什么问题?

我需要每次点击生成报告button时生成自动报告。 我正在使用sqlyog,我的表名是最终的,我的数据库名称是等我的数据库表项不是静态的,所以我需要一个自动化的报告。

我正在使用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"); } } 

我在你的数据库中创build了一个相同的表,并尝试运行你的代码。 我可以创buildExcel文件,而无需更改代码。 只是不同的是我使用不同的驱动程序(“oracle.jdbc.driver.OracleDriver”)。 所以首先检查你的数据库连接。 如果成功,那么其余的代码应该可以正常工作。
请发布更具体的例外,如果有的话。 这将有助于解决问题。 还有一件事你已经使用从第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文件中添加标题,请使用以下代码。

您应该在工作表中创build一个合并区域。

您可以使用CellRangeAddress提供可以合并的范围。 它以startRow,endRow,startCol,endCol为值来创build单元格区域地址。

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

我已经使用alignment来alignment中心的内容。

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

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

你可以通过java代码使用apache poi库类(如HSSFSheet,HSSFRow)来创buildExcel表。

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

使用上面的程序,你可以创build一个Excel工作表。

看来你一遍又一遍覆盖同一行和单元格对象。 对于每个新的单元格,你需要创build一个新的对象:

  //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);