时间:2021-07-01 10:21:17 帮助过:14人阅读
java代码:
import java.io.File; import java.io.IOException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.GregorianCalendar; import java.util.Random; import org.java.team.Writeteam; import org.java.tianbao.Tianbao; import org.java.util.DBUtil; import org.java.xiaowailianjie.WriteXiaowailianjie; import jxl.Workbook; import jxl.write.Label; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; import jxl.write.WriteException; import jxl.write.biff.RowsExceededException; public class a { public static void main(String[] args) throws RowsExceededException, WriteException, IOException { //1. 导出Excel的路径 String filePath = "C:/export.xls"; WritableWorkbook wwb =null; Connection connection=DBUtil.getConnection(); List<excelTest> excelTests=new ArrayList<excelTest>(); PreparedStatement preparedStatement=null; String sql="select * from excelTest"; ResultSet resultSet=null; excelTest exce=null; try { wwb = Workbook.createWorkbook(new File(filePath)); } catch (Exception e) { e.printStackTrace(); } //创建Excel表的"学生"区域的数据 WritableSheet sheet = wwb.createSheet("学生",0);//或者rwb.getSheet(0)获取第一个区域 try { //2. 连接数据库的几行代码 preparedStatement=connection.prepareStatement(sql); resultSet=preparedStatement.executeQuery(); while(resultSet.next()) { exce=new excelTest(); exce.setId(resultSet.getInt("id")); exce.setName(resultSet.getString("name")); exce.setNum(resultSet.getString("num")); excelTests.add(exce); }
sheet.addCell(new Label(0,0,"编号")); sheet.addCell(new Label(1,0,"姓名")); sheet.addCell(new Label(2,0,"学号")); for(int i = 0; i<excelTests.size(); i++){ //Number对应数据库的int类型数据 sheet.addCell(new jxl.write.Number(0,i+1,excelTests.get(i).getId())); //Label对应数据库String类型数据 sheet.addCell(new Label(1,i+1,excelTests.get(i).getName())); sheet.addCell(new Label(2,i+1,excelTests.get(i).getNum())); } wwb.write(); } catch (SQLException e) { e.printStackTrace(); }finally{ wwb.close(); } } }
数据库截图
导出的excel表格截图
java输出mysql到excel表格的简单用法
标签:.text rest ble 编号 cti mysql throw string类 and