`
beibei1022
  • 浏览: 4531 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

JAVA读取数据库数据导出excel文件

阅读更多
  1. 运行环境描述:
    java工程
    tomcat服务
    mysql数据库
  2. 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配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	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">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
        <servlet>
  	   <servlet-name>excel</servlet-name>
  	   <servlet-class>com.daochu.excel.DaoChuServlet</servlet-class>
  	</servlet>
  	<servlet-mapping>
  		<servlet-name>excel</servlet-name>
  		<url-pattern>/excel</url-pattern>
  	</servlet-mapping>
</web-app>

 

4.访问方式:
http://ip地址:端口号/项目名称/excel?id=5
例:
http://localhost:8989/daochu/excel?id=5

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

  • jxl.jar (688 KB)
  • 下载次数: 11
  • 大小: 124.3 KB
分享到:
评论
1 楼 贝塔ZQ 2017-01-20  
Java实现读取文档,也可以用插件进行实现,pageoffice插件就可以,主要是通过调用本地office进行在线处理文档的,任何类型的数据库都可以支持。需要的可以查查看。

相关推荐

Global site tag (gtag.js) - Google Analytics