import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

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 org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

//复制excel文件

public void copy(File sourceFile, File targetFile) {
if (targetFile.exists()) {
targetFile.delete();
}
int len = (int) sourceFile.length();
byte[] data = new byte[len];
FileInputStream input = null;
FileOutputStream output = null;
try {
input = new FileInputStream(sourceFile);
output = new FileOutputStream(targetFile);
input.read(data);
output.write(data);
} catch (Exception ex) {
log.error("Cannot init student template {}", targetFile, ex);
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
}
}
if (output != null) {
try {
output.close();
} catch (IOException e) {
}
}
}
}

//向excel表中写入数据

public void writeExcel(List<Map> templateList, int m, String path) {
OutputStream out = null;
try {
// 获取总列数
int columnNumCount = m;
// 读取Excel文档
File finalXlsxFile = new File(path);
Workbook workBook = getWorkbok(finalXlsxFile);
// sheet 对应一个工作页
Sheet sheet = workBook.getSheetAt(0);
Sheet sheet1 = workBook.getSheetAt(1);
/**
* 删除原有数据,除了属性列
*/
int rowNumber = sheet.getLastRowNum(); // 第一行从0开始算
System.out.println("原始数据总行数,除属性列:" + rowNumber);
for (int i = 1; i <= rowNumber; i++) {
Row row = sheet.getRow(i);
sheet.removeRow(row);
}
/**
* 往Excel中写新数据
*/
for (int j = 0; j < templateList.size(); j++) {
// 创建一行:从第二行开始,跳过属性列
Row row = sheet.createRow(j + 1);
Row row1 = null;
// 得到要插入的每一条记录
Map dataMap = templateList.get(j);
String name = dataMap.get("name").toString();
String subjectName = null;
if(dataMap.containsKey("subjectName")){
subjectName = dataMap.get("subjectName").toString();
row1 = sheet1.createRow(j + 1);
}
for (int k = 0; k <= columnNumCount; k++) {
// 在一行内循环(列)
Cell first = row.createCell(0);
first.setCellValue(name);
if(subjectName != null){
Cell secend = row1.createCell(0);
secend.setCellValue(subjectName);
}
}
}
// 创建文件输出流,准备输出电子表格:这个必须有,否则你在sheet上做的任何操作都不会有效
out = new FileOutputStream(finalXlsxFile.getAbsolutePath());
workBook.write(out);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.flush();
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("数据导出成功");
}

/**
* 判断Excel的版本,获取Workbook
*
* @param in
* @param filename
* @return
* @throws IOException
*/
public static Workbook getWorkbok(File file) throws IOException {
Workbook wb = null;
FileInputStream in = new FileInputStream(file);
if (file.getName().endsWith(EXCEL_XLS)) { // Excel 2003
wb = new HSSFWorkbook(in);
} else if (file.getName().endsWith(EXCEL_XLSX)) { // Excel 2007/2010
wb = new XSSFWorkbook(in);
}
return wb;
}

复制excel表,往excel表中写入数据的更多相关文章

  1. POI向Excel中写入数据及追加数据

    import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import ...

  2. 计算机二级-C语言-程序填空题-190117记录-对文件的处理,复制两个文件,往新文件中写入数据。

    //给定程序的功能是,调用函数fun将指定源文件中的内容赋值到指定目标文件中,复制成功时函数返回1,失败时返回0,把复制的内容输出到终端屏幕.主函数中源文件名放在变量sfname中,目标文件名放在变量 ...

  3. PHP连接sqlserver的两种方法,向sqlserver2000中写入数据,中文乱码

    项目环境是php5.3.28 项目用的ThinkPHP3.2.3  已经mysql5.5数据库,要和另一个项目对接,需要连接sqlsever2000数据库进行一些操作. 第一种用php自带扩展连接数据 ...

  4. 向post请求中写入数据,最终保存在了HttpWebRequest.Params中

    一.向post请求中写入数据,最终保存在了HttpWebRequest.Params中: 1)如果存入的是IDictionary类型的字符串变量,如:“username=administrator”, ...

  5. 通过I2C总线向EEPROM中写入数据,记录开机次数

    没买板子之前,用protues画过电路图,实现了通过i2c总线向EEPROM中写入和读出数据. 今天,在自己买的板子上面写关于i2c总线的程序,有个地方忘了延时,调程序的时候很蛋疼.下面说说我对I2c ...

  6. 【转】从QDataStream向QByteArray中写入数据时的注意点(QT)

    最近发现从QDataStream向QByteArray中写入数据常常是写不进去的,通过查看QT的源码: QDataStream &operator>>(QDataStream &a ...

  7. POI往word模板中写入数据

    转: POI往word模板中写入数据 2018年03月24日 16:00:22 乄阿斗同學 阅读数:2977  版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn ...

  8. Verilog利用$fdisplay命令往文件中写入数据

    最近在做的事情是,用FPGA生成一些满足特定分布的序列.因此为了验证我生成的序列是否拥有预期的性质,我需要将生成的数据提取出来并且放到MATLAB中做数据分析. 但是网上的程序很乱,表示看不懂==其实 ...

  9. java实现赋值excel模板,并在新文件中写入数据,并且下载

    /** * 生成excel并下载 */ public void exportExcel(){ File newFile = createNewFile(); //File newFile = new ...

随机推荐

  1. Kattis - Speed Limit

    Speed Limit Bill and Ted are taking a road trip. But the odometer in their car is broken, so they do ...

  2. 带你学C带你飞!

    C语言免费课程推荐:带你学C带你飞! 想学习C语言,首先就要了解什么是C语言: C语言是一门通用计算机编程语言,应用广泛.C语言的设计目标是提供一种能以简易的方式编译.处理低级存储器.产生少量的机器码 ...

  3. IDEA里面的facets和artifacts的讲解

    Facets: Facets表述了在Module中使用的各种各样的框架.技术和语言.这些Facets让Intellij IDEA知道怎么对待module内容,并保证与相应的框架和语言保持一致. 使用F ...

  4. Java 应用运维

    作者:http://blogread.cn/it/article/4918?f=wb 出处:http://blogread.cn/it/article/4918?f=wb Java应用运维    出处 ...

  5. hdu 1693 插头dp入门

    hdu1693 Eat the Trees 题意 在\(n*m\)的矩阵中,有些格子有树,没有树的格子不能到达,找一条或多条回路,吃完所有的树,求有多少种方法. 解法 这是一道插头dp的入门题,只需要 ...

  6. CF508E (贪心+搜索+构造)

    题目大意:让你构造一个括号序列,括号匹配的方式类似于栈,给出从左数每个括号 到和它匹配的右括号的 最小和最大距离,让你输出一个合法括号序列 看错题了以为是二分图,然后写了搜索 贪心发现如果距离往小了填 ...

  7. selenium+xpath获取href的坑

    先上HTML文档 <html> <body> <a href="http://www.example.com">Example</a> ...

  8. [剑指offer] 8+9. 跳台阶+变态跳台阶 (递归 时间复杂度)

    跳台阶是斐波那契数列的一个典型应用,其思路如下: # -*- coding:utf-8 -*- class Solution: def __init__(self): self.value=[0]*5 ...

  9. Vijos 1456 最小总代价 (状压dp)

    看到这道题n只有16,就可以想到状压dp 每个人只有经过或者没经过,那就用1表示经过,0表示没经过 但是不是当前在谁那里,所以再加一维来记录 所以f[state][i]表示在物品在i,当前的状态是st ...

  10. git与github的连接流程

    https://blog.csdn.net/sssssuuuuu666/article/details/78565381 https://www.cnblogs.com/wzd5230/p/49064 ...