java POI 将txt文件导入到excel中
来源:互联网 发布:双筒望远镜 知乎 编辑:程序博客网 时间:2024/05/18 01:33
简单demo
数据结构:包含三个string类型的数据,且都被逗号分隔开,导入到excel中
ValueObject .java 类
package XlsDto2Excel;public class ValueObject {private String name; /**名称**/private String value; /**值**/private String unit; /**单位**/private boolean isComment = false;private String comment;public ValueObject(String name,String value, String unit) {this.value = value;this.unit = unit; this.name = name;}public ValueObject(String comment) {this.comment = comment;this.isComment = true;}public boolean isComment() {return isComment;}public String getComment() {return comment != null ? comment : "";}public String getName() {return name != null ? name : "";}public String getValue() {return value != null ? value : "";}public String getUnit() {return unit != null ? unit : "";}}allmain.java
package XlsDto2Excel;import org.apache.poi.hssf.usermodel.HSSFRow;import org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.util.*;public class allmain {public static void main(String[] args) throws IOException {Scanner in = null; List<String> inStrs = new ArrayList<>(); try { in = new Scanner(new File("E:/data_stats_0.txt")); while (in.hasNextLine()) { inStrs.add(in.nextLine()); } } catch (Exception e) { e.printStackTrace(); } finally { if (in != null) { in.close(); } } List<ValueObject> vals = new ArrayList<>(); for (String str : inStrs) { str = str.trim(); if (str.startsWith("**") && str.endsWith("**")) { vals.add(new ValueObject(str)); } else { String[] res = str.split(",", 3); if (res.length == 3) { vals.add(new ValueObject(res[0], res[1], res[2])); } } } HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet = workbook.createSheet("default"); for (int i = 0; i < vals.size(); ++i) { HSSFRow row = sheet.createRow(i); ValueObject val = vals.get(i); if (val.isComment()) { row.createCell(7).setCellValue(val.getComment()); } else { row.createCell(7).setCellValue(val.getName()); row.createCell(8).setCellValue(val.getValue()); row.createCell(9).setCellValue(val.getUnit()); } } try { FileOutputStream out = new FileOutputStream(new File("E:/data_stats_0.xls")); workbook.write(out); out.close(); } catch (Exception e) { e.printStackTrace(); } workbook.close();}}
0 0
- java POI 将txt文件导入到excel中
- java中使用poi导入excel文件
- Java 利用POI实现将数据库中内容导入到EXcel中
- Java 利用POI实现将数据库中内容导入到EXcel中
- 将TXT文件中的内容正确导入到Excel表格中不混乱的方法
- 将txt文档数据导入到excel中
- POI读入excel文件到Java中
- java poi技术将Excel文件内容导入数据库
- 将excel文件导入到数据库中
- 将excel文件导入到dataset中
- java poi Excel文件导入
- java中用poi导入excel文件到数据库总结
- 将txt文件导入到mysql表中
- Java 读取excel指定行列数据以及将数据保存到txt文件中
- POI 导入导出Excel文件到数据库
- POI 导入导出Excel文件到数据库
- Aapche POI txt 导入excel
- Java使用Apache poi 导入Excel文件
- Cocos Play提升3倍转化率:手游微端革命
- LeetCode:Bulls and Cows
- dom4j 解析xml字符串 去除.DTD校验
- 印章图片制作操作手册
- java使用域账号登陆,ldap协议
- java POI 将txt文件导入到excel中
- maven compile的web项目时指定/WEB-INF/lib 目录作为额外的库目录
- SVN服务器搭建和使用(一)
- 个人博客建立(2)
- Servlet文件上传(ServletFIleUpload,DiskFileItemFactory,FileItem)
- 出来三个月了,随便写点什么
- 离散系统的差分方程、冲激响应和卷积分析
- UML图之类图,对象图和包图
- JS登录验证