1:所需jar包

2:Mysql数据库表内容如下:

3:代码结构如下:

(1)User.java

public class User {
private int id;
private String name;
private String no;
private String nativePlace;
private String edu;
private Double math;
private Double computer;
private Double english;
private Double sumcount;
private Double avgcount; //setter-getter ...
}

(2)JdbcUtil.java

public class JdbcUtil {

    private static final String URL = "jdbc:mysql://localhost:3306/test";
private static final String USER = "root";
private static final String PASSWORD = "mysql"; private JdbcUtil() {
} static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException ex) {
Logger.getLogger(JdbcUtil.class.getName()).log(Level.SEVERE, null, ex);
}
} public static Connection getConnection() throws Exception {//建立连接
return DriverManager.getConnection(URL, USER, PASSWORD);
} public static void free(ResultSet rs, Statement st, Connection conn) {//释放资源
try {
if (rs != null) {
rs.close();
}
} catch (SQLException ex) {
Logger.getLogger(JdbcUtil.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
if (st != null) {
st.close();
}
} catch (SQLException ex) {
Logger.getLogger(JdbcUtil.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException ex) {
Logger.getLogger(JdbcUtil.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
}
}

(3)UserService.java

public class UserService {

    public static List<User> getUserList(){
User user;
List<User> list = new ArrayList<>();
String sql = "select id,name,no,nativeplace,edu,math,computer,english,sumcount,avgcount from t_user";
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = JdbcUtil.getConnection();
ps = conn.prepareStatement(sql);
rs = ps.executeQuery(); while(rs.next()){
user = new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setNo(rs.getString("no"));
user.setNativePlace(rs.getString("nativeplace"));
user.setEdu(rs.getString("edu"));
user.setMath(rs.getDouble("math"));
user.setComputer(rs.getDouble("computer"));
user.setEnglish(rs.getDouble("english"));
user.setSumcount(rs.getDouble("sumcount"));
user.setAvgcount(rs.getDouble("avgcount")); list.add(user);
}
} catch (Exception ex) {
Logger.getLogger(UserService.class.getName()).log(Level.SEVERE, null, ex);
}finally{
JdbcUtil.free(rs, ps, conn);
}
return list;
} }

(4)Poitest.java

public class Poitest {

    /**
* @param args the command line arguments
*/
public static void main(String[] args) {
InputStream inputStream = null;
OutputStream outputStream =null;
try {
inputStream = new FileInputStream(new File("E:\\hello_temp.xls"));
outputStream = new FileOutputStream(new File("E:\\hello1.xls")); writeToExcelByTemp2(inputStream,outputStream); System.out.println("成功生成");
} catch (FileNotFoundException ex) {
Logger.getLogger(Poitest.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Poitest.class.getName()).log(Level.SEVERE, null, ex);
}finally{
if(null!=outputStream){
try {
outputStream.close();
} catch (IOException ex) {
Logger.getLogger(Poitest.class.getName()).log(Level.SEVERE, null, ex);
}
}
if(null!=inputStream){
try {
inputStream.close();
} catch (IOException ex) {
Logger.getLogger(Poitest.class.getName()).log(Level.SEVERE, null, ex);
}
}
} } /**
* 根据模版生成Excel
* @param inputStream
* @param outputStream
* @throws IOException
*/
public static void writeToExcelByTemp2(InputStream inputStream, OutputStream outputStream) throws IOException{
List<User> list = UserService.getUserList();
int length = list.size();
//New Workbook
Workbook wb = new HSSFWorkbook(inputStream); //New Sheet
Sheet sheet = wb.getSheetAt(0); int curRowIndex = 0;
for(int rowIndex=1; rowIndex<=length;rowIndex++){
curRowIndex = rowIndex;
//获取一行,如果为空则新建
Row row = sheet.getRow(rowIndex);
if(row == null){
row = sheet.createRow(rowIndex);
} User user = list.get(rowIndex-1);
//根据user的对象个数创建列数
for(int cellNum=0; cellNum<10;cellNum++){
Cell cell = row.getCell(cellNum);
if(cell==null){
cell = row.createCell(cellNum);
}
switch(cellNum){
case 0:
cell.setCellValue(user.getId());
break;
case 1:
cell.setCellValue(user.getName());
break;
case 2:
cell.setCellValue(user.getNo());
break;
case 3:
cell.setCellValue(user.getNativePlace());
break;
case 4:
cell.setCellValue(user.getEdu());
break;
case 5:
cell.setCellValue(user.getMath());
break;
case 6:
cell.setCellValue(user.getComputer());
break;
case 7:
cell.setCellValue(user.getEnglish());
break;
case 8:
cell.setCellFormula("SUM(F"+(rowIndex+1)+":H"+(rowIndex+1)+")");
break;
case 9:
cell.setCellFormula("I" + (rowIndex+1) +"/3");
break;
}
}
} curRowIndex++; Row row = sheet.createRow(curRowIndex);
Cell cell0 = row.createCell(0);
cell0.setCellValue("总计");
Cell cell5 = row.createCell(5);
cell5.setCellFormula("SUM(F2:"+"F"+(curRowIndex)+")");
Cell cell6 = row.createCell(6);
cell6.setCellFormula("SUM(G2:"+"G"+(curRowIndex)+")");
Cell cell7 = row.createCell(7);
cell7.setCellFormula("SUM(H2:"+"H"+(curRowIndex)+")");
Cell cell8 = row.createCell(8);
cell8.setCellFormula("SUM(I2:"+"I"+(curRowIndex)+")");
Cell cell9 = row.createCell(9);
cell9.setCellFormula("AVERAGE(J2:"+"J"+(curRowIndex)+")"); wb.write(outputStream);
}
}

Excel模版:

生成结果:

POI3.10 根据Excel模版导出数据测试的更多相关文章

  1. JAVA实现Excel导出数据(以写好的Excel模版导出)

    工作中经常会有将后台数据以Excel导出的功能. 简单的方法有将response的contentType设置为application/vnd.ms-excel: 或在JSP页面直接设置成: <% ...

  2. [poi使用]使用excel模版导出excel

    ​ Apache POI是基于Office Open XML标准(OOXML)和Microsoft的OLE 2复合文档格式(OLE2)处理各种文件格式的开源项目.简而言之,您可以使用Java读写MS ...

  3. POI3.10读取Excel模板填充数据后生成新的Excel文件

    private final DecimalFormat df = new DecimalFormat("#0.00"); public void test(){ String fi ...

  4. (6) 如何用Apache POI操作Excel文件-----POI-3.10的一个和注解(comment)相关的另外一个bug

    如果POI-3.10往一个工作表(sheet)里面插入数据的话,需要注意了,其有一个不太被容易发现的bug. 被插入的工作表(sheet)里面的单元格没有包含任何的注解(comment)的时候,插入一 ...

  5. jxls使用模版导出Excel

    /**     * 使用模版导出Excel     */    @SuppressWarnings({ "unchecked", "deprecation" } ...

  6. java+jxls利用excel模版进行导出

    大多时候会出现需要导出excel的功能,利用poi可以实现简单的导出,可以说poi的功能非常强大可以做到细节的定制化操作,但相对于在office操作excel,利用poi完全生成excel会显得非常复 ...

  7. 按模版导出Excel

    实现效果: excel模版: ExcelHandle.java package com.common.utils; import java.io.File; import java.io.FileIn ...

  8. NPOI的使用Excel模板导出

    private string ExportScMeeting(DataTable source) { string templateFile = Server.MapPath(@"Excel ...

  9. C#巧用Excel模版变成把Table打印出来

    将一个做好的Excel模版,通过程序填上数据然后打印出来这个需求有两种方法一种是通过代码打开Excel模版然后填入数据然后再打印. 第二种方法就是我将要介绍的 1.将Excel设置好格式另存为HTML ...

随机推荐

  1. PowerShell官方的MSDN

    https://msdn.microsoft.com/en-us/powershell/mt173057.aspx 官方还咋github上放置了  扩展模块. 比如 web iis部署.sqlserv ...

  2. 10个工具让你的 shell 脚本更强大

    10个工具让你的 shell 脚本更强大 很多人误以为shell脚本只能在命令行下使用.其实shell也可以调用一些GUI组件,例如菜单,警告框,进度条等等.你可以控制最终的输出,光标位 置还有各种输 ...

  3. Using HTML5 audio and video

    From:https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Using_HTML5_audio_and_video Using HTML5 ...

  4. windows多线程同步总结

    1.多线程同步与多线程互斥的关系 其实这也是我一直困扰的问题,在这里我只是说说我的理解.我的理解是多线程互斥是针对于多线程资源而言的. 而多线程同步是针对于多线程时序问题.由于线程的并发性导致其运行时 ...

  5. <转载>僵尸进程

    转载http://www.cnblogs.com/scrat/archive/2012/06/25/2560904.html 什么是僵尸进程 僵尸进程是指它的父进程已经退出(父进程没有等待(调用wai ...

  6. 整个Html内容以邮件的方式发送出去(取出标签包含的用户输入信息)

    需求是一个html的调查问卷,在调查问卷完成后,将问卷页面(包括用户填写的答案)完整的发送给领导. 问题出现了 填写的时候用的是jquery赋值的方法 ,比如text文本.textrear用的是val ...

  7. STS(Spring Tool Suite)使用前准备

    sts 的基础框架拿的eclipse的,你可以理解为eclipse + spring插件的高级升华版.在使用上可以很大限度的参考eclipse的操作. 首先,调整字体. 中文很麻烦的,因为编码问题.习 ...

  8. java与.net比较学习系列(4) 运算符和表达式

    上一篇总结了java的数据类型,得到了冰麟轻武等兄弟的支持,他们提出并补充了非常好的建议,在这里向他们表示感谢.在后面的文章中,我会尽力写得更准确和更完善的,加油! 另外,因为C#是在java之后,也 ...

  9. Swift 全功能的绘图板开发

    要做一个全功能的绘图板,至少要支持以下这些功能: 支持铅笔绘图(画点) 支持画直线 支持一些简单的图形(矩形.圆形等) 做一个真正的橡皮擦 能设置画笔的粗细 能设置画笔的颜色 能设置背景色或者背景图 ...

  10. Filter简单介绍

    一.简单介绍 Filter也称为过滤器,WEB开发者通过Filter技术.对webserver管理的全部web资源:比如Jsp, Servlet, 静态图片文件或静态 html 文件等进行拦截.从而实 ...