java mysql 导出excel文件怎么打开文件_JAVA读取数据库数据导出excel文件

这篇博客介绍了一个Java工程中使用Servlet从MySQL数据库导出数据到Excel文件的过程。首先,通过ConnectionPooling初始化数据库连接,然后执行SQL查询获取数据。接着,创建Excel文件并利用JXL库将查询结果写入文件。最后,通过response.sendRedirect重定向用户下载生成的Excel文件。
摘要由CSDN通过智能技术生成

运行环境描述:

java工程

tomcat服务

mysql数据库

java导出servlet类:

package com.daochu.excel;

import java.io.File;

import java.io.FileOutputStream;

import java.io.FileWriter;

import java.io.IOException;

import java.io.OutputStream;

import java.io.PrintWriter;

import java.sql.ResultSet;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import jxl.Workbook;

import jxl.write.Label;

import com.broadway.db.ConnectionManager;

import com.broadway.db.ConnectionPooling;

public class DaoChuServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

doPost(request, response);

}

@SuppressWarnings("deprecation")

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

ConnectionPooling.init(""); // 连接数据库初始化时

ConnectionManager conn = new ConnectionManager(ConnectionPooling.getConnection());

ResultSet rs = null;

try {

String id = request.getParameter("id") == null ?"" : request.getParameter("id");

if(!id.equals("")){

String sql = "select * from lotteryfenxi where id = '"+id+"'";

rs = conn.executeQuery(sql);

// 新建Excel文件

String filePath = request.getRealPath("Test.xls");

System.out.println(filePath);

File myFilePath = new File(filePath);

if (!myFilePath.exists())

myFilePath.createNewFile();

FileWriter resultFile = new FileWriter(myFilePath);

PrintWriter myFile = new PrintWriter(resultFile);

resultFile.close();

// 用JXL向新建的文件中添加内容

OutputStream outf = new FileOutputStream(filePath);

jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(outf);

//生成名为“sheettest”的工作表,参数0表示这是第一页

jxl.write.WritableSheet ws = wwb.createSheet("sheettest", 0);

int i = 0;

int j = 0;

for (int k = 0; k < rs.getMetaData().getColumnCount(); k++) {

ws.addCell(new Label(k, 0, rs.getMetaData().getColumnName(k + 1)));

}

//getMetaData() 获取此 ResultSet 对象的列的编号、类型和属性。

//getColumnCount()返回此 ResultSet 对象中的列数。

System.out.println("列数:"+rs.getMetaData().getColumnCount());

while (rs.next()) {

for (int k = 0; k < rs.getMetaData().getColumnCount(); k++) {

ws.addCell(new Label(k, j + i + 1, rs.getString(k + 1)));

}

i++;

}

wwb.write();

wwb.close();

}

rs.close();

conn.closeRs();

} catch (Exception e) {

e.printStackTrace();

} finally {

conn.closeConn();

}

response.sendRedirect("Test.xls");

}

}

3.  web.xml配置:

xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

index.jsp

excel

com.daochu.excel.DaoChuServlet

excel

/excel

5.连接数据库的方式多种多样,我只用了我自己的连接方式,大家可以采用自己连接数据库的方式,只要能正常访问就ok

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值