利用excel模板,将数据填充到excel中
package com.excel;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.commons.io.FileUtils;
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;
public class WriteDataToExcel {
public static void main(String[] args) {
writeExcel("C:\\Users\\admin\\Desktop\\测试.xls");
}
public static void writeExcel(String finalXlsxPath) {
OutputStream out = null;
// 读取Excel文档
File finalXlsxFile = createNewFile(finalXlsxPath);//复制模板,
Workbook workBook = null;
try {
workBook = getWorkbok(finalXlsxFile);
} catch (IOException e1) {
e1.printStackTrace();
}
// sheet 对应一个工作页 插入数据开始 ------
Sheet sheet = workBook.getSheetAt(0);
Row row6 = sheet.getRow(5);// 获取到第6行
Cell cell5 = row6.getCell(4);// 6行 5列
cell5.setCellValue("6行5列的值");
Cell cell6 = row6.getCell(5);// 6行 6列
cell6.setCellValue("6行6列的值");
Cell cell7 = row6.getCell(6);// 6行 7列
cell7.setCellValue("6行7列的值");
Cell cell8 = row6.getCell(7);// 6行 8列
cell8.setCellValue("6行8列的值");
Cell cell9 = row6.getCell(8);// 6行 9列
cell9.setCellValue("6行9列的值");
Cell cell10 = row6.getCell(9);// 6行 10列
cell10.setCellValue("6行10列的值");
Row row7 = sheet.getRow(6);
Cell cell11 = row7.getCell(4);// 7行 5列
cell11.setCellValue("7行5列的值");
//插入数据结束
try {
out = new FileOutputStream(finalXlsxFile);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
try {
workBook.write(out);
} catch (IOException e1) {
e1.printStackTrace();
}
try {
if (out != null) {
out.flush();
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 判断excel格式版本
*
* @param file
* @return
* @throws IOException
*/
public static Workbook getWorkbok(File file) throws IOException {
Workbook wb = null;
FileInputStream in = new FileInputStream(file);
if (file.getName().endsWith(".xls")) { // Excel 2003
wb = new HSSFWorkbook(in);
} else if (file.getName().endsWith("xlsx")) { // Excel 2007/2010
wb = new XSSFWorkbook(in);
}
return wb;
}
private static File createNewFile(String path) {
// 读取模板,并赋值到新文件************************************************************
// 文件模板路径
File file = new File(path);
if (!file.exists()) {
System.out.println("原模板文件不存在");
}
// 保存文件的路径
String realPath = file.getParent();
// 新的文件名
String newFileName = "报表-" + System.currentTimeMillis()
+ ".xls";
// 判断路径是否存在
File dir = new File(realPath);
if (!dir.exists()) {
dir.mkdirs();
}
// 写入到新的excel
File newFile = new File(realPath, newFileName);
try {
newFile.createNewFile();
// 复制模板到新文件
FileUtils.copyFile(file, newFile);
} catch (Exception e) {
e.printStackTrace();
}
return newFile;
}
}
jar:
poi-3.6-20091214.jar
poi-ooxml-3.6-20091214.jar
commons-io-1.4.jar
利用excel模板,将数据填充到excel中的更多相关文章
- C# 将Excel里面的数据填充到DataSet中
/// <summary> /// 将Excel表里的数据填充到DataSet中 /// </summary> /// <param name="filenam ...
- Aspose.cell中的Excel模板导出数据
//Excel模板导数据(Eexcel中根据DataTable中的个数,给多个Sheet中的模板赋值) public void DataSetToManyExcel(string fileName, ...
- kettle 使用excel模板导出数据
通过excel进行高速开发报表: 建设思路: 1.首先制订相关的execl模板. 2.通过etl工具(kettle)能够高速的 将数据库中的数据按excel模板导出成新的excel就可以. 当中ket ...
- C# Winform Excel的导出,根据excel模板导出数据
namespace dxhbskymDemo { public partial class ExcelForm : DevExpress.XtraEditors.XtraForm { public E ...
- .net dataGridView当鼠标经过时当前行背景色变色;然后【给GridView增加单击行事件,并获取单击行的数据填充到页面中的控件中】
1.首先在前台dataGridview属性中增加onRowDataBound属性事件 2.然后在后台Observing_RowDataBound事件中增加代码 protected void Obser ...
- Net 自定义Excel模板导出数据
转载自:http://www.cnblogs.com/jbps/p/3549671.html?utm_source=tuicool&utm_medium=referral 1 using Sy ...
- java实现赋值excel模板,并在新文件中写入数据,并且下载
/** * 生成excel并下载 */ public void exportExcel(){ File newFile = createNewFile(); //File newFile = new ...
- Winform .NET 利用NPOI导出大数据量的Excel
前言:公司让做一个导出数据到Excel的小工具,要求是用户前端输入sql语句,点击导出按钮之后,将数据导出到Excel,界面如图所示:文件下端显示导出的进度 遇到的问题: 1.使用NPOI进行Exce ...
- excel文件与txt文件互转,并且把excel里的数据导入到oracle中
一.excel文件转换成txt文件的步骤 a.首先要把excel文件转换成txt文件 1.Excel另存为中已经包含了TXT格式,所以我们可以直接将Excel表格另存为TXT格式,但是最后的效果好像不 ...
- sqlserver怎么将excel表的数据导入到数据库中
在数据库初始阶段,我们有些数据在EXCEL中做好之后,需要将EXCEL对应列名(导入后对应数据库表的字段名),对应sheet(改名为导入数据库之后的表名)导入指定数据库, 相当于导入一张表的整个数据. ...
随机推荐
- Execution failed for task ':compileDebugAidl'.
昨天终于升级了下Ubuntu系统到16.04LTS,之前是12.04LTS(导致内网一些同事开发的网址无法打开,以及其他工具软件无法安装). 安装完android开发工具,运行之前的project,出 ...
- ubuntu 安装jdk 的两种方式:
:通过ppa(源) 方式安装. 2:通过官网下载安装包安装. 这里推荐第1种,因为可以通过 apt-get upgrade 方式方便获得jdk的升级 使用ppa/源方式安装 1.添加ppa sudo ...
- springMVC4(7)模型视图方法源代码综合分析
在完整web开发中.springMVC主要充当了控制层的角色.它接受视图层的请求.获取视图层请求数据,再对数据进行业务逻辑处理.然后封装成视图层须要的模型数据,再将数据导向到jsp等视图界面. 在前面 ...
- tmunx error:invalid option: status-utf8 invalid option: utf8
修改为:set-window-option -gq mouse off set-window-option -gq mode-mouse off set-option -gq status-utf8 ...
- 处理器 趣事 CPU/GPU/TPU/DPU/BPU
有消息称,阿里巴巴达摩院正在研发一款神经网络芯片——Ali-NPU,主要运用于图像视频分析.机器学习等AI推理计算.按照设计,这款芯片性能将是目前市面上主流CPU.GPU架构AI芯片的10倍,而制造成 ...
- R语言|数据特征分析
对数据进行质量分析以后,接下来可通过绘制图表.计算某些特征量等手段进行数据的特征分析. 主要通过分布分析.对比分析.统计量分析.周期性分析.贡献度分析.相关性分析等角度进行展开. 2.1 分布分析 分 ...
- Introducing Apache Spark Datasets(中英双语)
文章标题 Introducing Apache Spark Datasets 作者介绍 Michael Armbrust, Wenchen Fan, Reynold Xin and Matei Zah ...
- python 信息同时输出到控制台与文件
python编程中,往往需要将结果用print等输出,如果希望输出既可以显示到IDE的屏幕上,也能存到文件中(如txt)中,该怎么办呢? 方法1 可通过日志logging模块输出信息到文件或屏幕.但可 ...
- ImportError: No module named 'requests.packages.urllib3'
场景:CentOS 7 运行微信告警脚本报错 原因:requests库版本问题 解决方法: sudo pip install requests urllib3 pyOpenSSL --force -- ...
- 【ORACLE】SQL查询出每个组中的第一条记录
CREATE TABLE [TestTable] ( ) NOT NULL , ) NOT NULL , ) ))) GO ALTER TABLE [TestTable] ADD PRIMARY KE ...