package com.java.activiti.util;import java.io.FileOutputStream;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.ResultSetMetaData;import org.apache.poi.hssf.usermodel.HSSFWorkbook;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 com.mysql.jdbc.Connection;import com.mysql.jdbc.Statement;/** * Created by zhxn on 2018/10/30. */public class FromOneSheetToExcel1 { //TODO 需要将url\用户名\密码修改为自己的 public final static String url = "jdbc:mysql://localhost:3306/subway"; // 数据库URL public final static String user = "root"; // 数据库用户名 public final static String password = "123456"; // 数据库密码 // 把数据库单张表信息导入到Excel表中 public static void main(String[] args) throws Exception { // 架子啊数据库驱动 Class.forName("com.mysql.jdbc.Driver"); // 连接数据库// Connection con = (Connection) DriverManager.getConnection(url, user,// password);// // 创建Excel表。// Workbook book = new HSSFWorkbook();// // String Table_Name = "result";// // Sheet sheet = book.createSheet(Table_Name);//// Statement st = (Statement) con.createStatement();// // 创建sql语句,对team进行查询所有数据// String sql = "select t.id,t.para_org_leaderships_id,t.PAPER_ID,t.OBJ_RESULT,t.SUB_RESULT,t.TOTAL_RESULT from"// + " (select id,para_org_leaderships_id,PAPER_ID,OBJ_RESULT,SUB_RESULT,TOTAL_RESULT from "// + ""+Table_Name+" union select '成绩编号','组织架构参数表编号','试卷编号','客观题成绩','主观题成绩','总成绩') "// + "t order by t.id desc" ;// ResultSet rs = st.executeQuery(sql);// // 设置表头信息(写入Excel左上角是从(0,0)开始的)// Row row1 = sheet.createRow(0);// ResultSetMetaData rsmd = rs.getMetaData();// int colnum = rsmd.getColumnCount();// // // 设置表格信息// int idx = 0;// while (rs.next()) {// // 行// Row row = sheet.createRow(idx++);// for (int i = 1; i <= colnum; i++) {// String str="";// str = rs.getString(i);// // 单元格// Cell cell = row.createCell(i-1);// // 写入数据// cell.setCellValue(str);// }// }// // 保存// book.write(new FileOutputStream("D://" + Table_Name + ".xls")); }}
或者
// @RequestMapping("/numericalStatement")// public void numericalStatement(HttpServletResponse resp)throws Exception{// Connection con = (Connection) DriverManager.getConnection(FromOneSheetToExcel1.url, FromOneSheetToExcel1.user,// FromOneSheetToExcel1.password);// Workbook book = new HSSFWorkbook();// String Table_Name = "result";// Sheet sheet = book.createSheet(Table_Name);//// Statement st = (Statement) con.createStatement();// // 创建sql语句,对team进行查询所有数据// String sql = "select t.id,t.para_org_leaderships_id,t.PAPER_ID,t.OBJ_RESULT,t.SUB_RESULT,t.TOTAL_RESULT from"// + " (select id,para_org_leaderships_id,PAPER_ID,OBJ_RESULT,SUB_RESULT,TOTAL_RESULT from "// + ""+Table_Name+" union select '成绩编号','组织架构参数表编号','试卷编号','客观题成绩','主观题成绩','总成绩') "// + "t order by t.id desc" ;// ResultSet rs = st.executeQuery(sql);// // 设置表头信息(写入Excel左上角是从(0,0)开始的)// Row row1 = sheet.createRow(0);// ResultSetMetaData rsmd = rs.getMetaData();// int colnum = rsmd.getColumnCount();// for (int i = 1; i <= colnum; i++) {// String name = rsmd.getColumnName(i);// // System.err.println(name);// // 单元格// Cell cell = row1.createCell(i - 1);// // 写入数据// cell.setCellValue(name);// }// // 设置表格信息// int idx = 1;// while (rs.next()) {// // 行// Row row = sheet.createRow(idx++);// for (int i = 1; i <= colnum; i++) {// String str="";// str = rs.getString(i);// // 单元格// Cell cell = row.createCell(i-1);// // 写入数据// cell.setCellValue(str);// }// }// // 保存// book.write(new FileOutputStream("D://" + Table_Name + ".xls"));// }