java创建excel表格并将数据库查询结果写入到excel中

在java开发中,有时需要创建需要将数据库查询结果导入到excel表格中,这时候就需要先创建excel,然后将数据库查询的字段循环插入到excel对应的列中即可。

public class Demo(){
@Autowired
private DemoMapper demoMapper;

//创建excel并将数据库查询结果导入到excel方法 createExcel()
public void createExcel(){
    File file = new File(/home/vlog/统计表.xls);
    //创建一个webbook,对应一个excel文件
    HSSFWorkbook wb = new HSSFWorkbook();
    //在webbook中添加一个sheet
    HSSFSheet sheet =wb.createSheet("统计表");
    //在sheet中添加表头第0行
    HSSFRow row = sheet.creatRow(0);
    //创建单元格,并设置表头内容,格式为居中
    HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);
        row.createCell(0).setCellValue("日期");
        row.createCell(1).setCellValue("账号");
        row.createCell(2).setCellValue("电话");
    List<DemoBean> list = demoMapper.queryReocord();
    if (list.size() >0){
        for (int i =0; i < list.size(); i++){
            //从第二行开始创建行,第一行是表头
            row = sheet.creatRow(i+1);
            if (list.get(0).getDate()!= null){
                row.createCell(0).setCellValue(list.get(0).getDate())
            }
            if (list.get(0).getAcct()!= null){
                row.createCell(1).setCellValue(list.get(0).getAcct())
            }
            if (list.get(0).getTel()!= null){
                row.createCell(2).setCellValue(list.get(0).getTel())
            }
        }
    }  
    //将数据写入到excel表格中
    tryFileOutputStream output = new tryFileOutputStream(file){
        wb.write(output);
        output.close();
    }catch(IOException e){
        e.printStackTrace();
    }
 } 
}

上文中用到的DemoBean实体类,是需要查询的数据库的表的实体,根据实际的情况替换成自己的实体类就行了,本文中的内容如下:

@Data
@Entity
@TableName("DEMO_TABLE")
public class DemoBean{
	private String date;
	private String acct;
	private String tel;
}

上文中用到的DemoMapper类,为对应的数据库表的mapper类,具体到本文中,如下:

public interface DemoMapper{
	List<DemoBean> queryReocord();
}

如上就完成了excel创建,并将数据库查询结果写入到excel功能。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
您可以使用 Apache POI 库来读取 Excel 表格的数据,并使用 JDBC 连接 MySQL 数据库并将数据写入。 以下是一个示例代码: ```java import java.io.FileInputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ExcelToMySQL { private static final String JDBC_URL = "jdbc:mysql://localhost:3306/your_database_name"; private static final String USERNAME = "your_username"; private static final String PASSWORD = "your_password"; public static void main(String[] args) { try (Connection connection = DriverManager.getConnection(JDBC_URL, USERNAME, PASSWORD)) { String excelFilePath = "path_to_your_excel_file.xlsx"; FileInputStream fileInputStream = new FileInputStream(excelFilePath); Workbook workbook = new XSSFWorkbook(fileInputStream); Sheet sheet = workbook.getSheetAt(0); for (Row row : sheet) { // Assuming the first column contains the data you want to insert into the database Cell cell = row.getCell(0); String data = cell.getStringCellValue(); String sql = "INSERT INTO your_table_name (column_name) VALUES (?)"; PreparedStatement statement = connection.prepareStatement(sql); statement.setString(1, data); statement.executeUpdate(); } workbook.close(); fileInputStream.close(); System.out.println("Data inserted successfully into MySQL database."); } catch (Exception e) { e.printStackTrace(); } } } ``` 请确保您已经将 `your_database_name` 替换为实际的数据库名,`your_username` 和 `your_password` 替换为数据库的用户名和密码,`path_to_your_excel_file.xlsx` 替换为实际的 Excel 文件路径,`your_table_name` 替换为要插入数据的表名,`column_name` 替换为要插入数据的列名。 这段代码将逐行读取 Excel 表格的数据,并将其插入到 MySQL 数据库。请根据您的实际需求进行适当修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xiaoEchoJava

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值