Apache POI 连接数据库生成excel
来源:互联网 发布:5g网络什么时候出 编辑:程序博客网 时间:2024/05/17 06:29
本章介绍了POI库与数据库的交互方式。有了JDBC帮助,可以从数据库中检索数据并插入数据来使用POI库电子表格。让我们考虑SQL操作MySQL数据库。
写入数据库
让我们假设数据表是 emp_tbl 存有雇员信息是从MySQL数据库 test 中检索。
30000
使用下面的代码从数据库中检索数据,并插入到同一个电子表格。
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.xssf.usermodel.XSSFCell;import org.apache.poi.xssf.usermodel.XSSFRow;import org.apache.poi.xssf.usermodel.XSSFSheet;import org.apache.poi.xssf.usermodel.XSSFWorkbook;public class ExcelDatabase { public static void main(String[] args) throws Exception { Class.forName("com.mysql.jdbc.Driver"); Connection connect = DriverManager.getConnection( "jdbc:mysql://localhost:3306/test" , "root" , "root" ); Statement statement = connect.createStatement(); ResultSet resultSet = statement .executeQuery("select * from emp_tbl"); XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet spreadsheet = workbook .createSheet("employe db"); XSSFRow row=spreadsheet.createRow(1); XSSFCell cell; cell=row.createCell(1); cell.setCellValue("EMP ID"); cell=row.createCell(2); cell.setCellValue("EMP NAME"); cell=row.createCell(3); cell.setCellValue("DEG"); cell=row.createCell(4); cell.setCellValue("SALARY"); cell=row.createCell(5); cell.setCellValue("DEPT"); int i=2; while(resultSet.next()) { row=spreadsheet.createRow(i); cell=row.createCell(1); cell.setCellValue(resultSet.getInt("eid")); cell=row.createCell(2); cell.setCellValue(resultSet.getString("ename")); cell=row.createCell(3); cell.setCellValue(resultSet.getString("deg")); cell=row.createCell(4); cell.setCellValue(resultSet.getString("salary")); cell=row.createCell(5); cell.setCellValue(resultSet.getString("dept")); i++; } FileOutputStream out = new FileOutputStream( new File("exceldatabase.xlsx")); workbook.write(out); out.close(); System.out.println( "exceldatabase.xlsx written successfully"); }}
让我们保存了上面的代码为ExcelDatabase.java。编译并从命令提示符执行它如下。
$javac ExcelDatabase.java$java ExcelDatabase
它会生成一个名为exceldatabase.xlsx在当前目录中的Excel文件并显示在命令提示符处输出以下。
exceldatabase.xlsx written successfully
exceldatabase.xlsx文件如下所示。
文章转载自:易百教程 [http:/www.yiibai.com]
本文标题:Apache POI数据库
本文地址:http://www.yiibai.com/apache_poi/apache_poi_database.html
0 0
- Apache POI 连接数据库生成excel
- 使用apache.poi生成Excel
- Apache POI练习生成EXCEL(1)
- Apache POI练习生成EXCEL(2)
- 简单使用Apache poi 生成excel
- 使用 Apache Jakarta POI 从 Excel 电子表格生成外部表
- 使用 Apache Jakarta POI 从 Excel 电子表格生成外部表
- 使用Apache POI生成指定格式的Excel文件
- Java中用Apache POI生成excel和word文档
- Apache POI生成Excel表格并下载的一些注意事项
- 使用Apache POI生成和解析Excel文件
- 【Java】使用Apache POI生成和解析Excel文件
- Apache POI 操作Excel
- apache poi解析excel
- [Apache POI] 操作Excel
- apache poi导出excel
- Apache poi excel调用
- Apache POI 操作Excel
- cut和grep结合案列(获取普通用户)
- 启用Xdebug使用WinCacheGrind分析脚本执行时间 .
- iOS的uinavigationController为null的原因及后果
- oracle入门之 Select查询语句(二)
- java-exercise-1 多线程下载的实现
- Apache POI 连接数据库生成excel
- jmeter安装配置
- 网络编程之inet_pton,inet_ntop,sock_ntop函数
- iOS Core Image Tutorial学习
- USB描述符和枚举总结 --自己总结
- hibernate4.0+版本和3.0+版本的区别总结
- 在Mac上配置adb命令
- (全过程)如何制作论坛网站,怎样免费制作论坛,制作论坛教程
- angularjs IE cahce