需求:有一个数据字典全量汇总表,其中第一个sheet为目录,包括编号和表名,第二个以后为表的明细。其中sheet名就是表名但无序,sheet内字段序号无序有空行

现在要求将其中101,104,107,111表中的格式列和字段名称以及表名取出,生成批量语句,要求按给的编号有序输出,字段出要有序并排除窄。

输出结果如下:

insert into t_export(export_id,owner,table_name,col_name,format) values(101,'T_A','scott','id','20180308001');
insert into t_export(export_id,owner,table_name,col_name,format) values(101,'T_A','scott','sal','2000');
insert into t_export(export_id,owner,table_name,col_name,format) values(101,'T_A','scott','20','张三');
insert into t_export(export_id,owner,table_name,col_name,format) values(101,'T_A','scott','remark','hello');
insert into t_export(export_id,owner,table_name,col_name,format) values(101,'T_A','scott','birthday','40479');
insert into t_export(export_id,owner,table_name,col_name,format) values(101,'T_A','scott','age','20');
.
.
.
至111

excel如下:

下载poi包poi-3.17.jar并引入eclipse的java工程,仅需要RowInfo.java,ShowExcel.java

RowInfo.java:

package pu;

public class RowInfo implements Comparable<RowInfo>{//实现字段排序

    private int rownumb;
private int expId;
private String tableName;
private String columnName;
private String formatInfo;
public RowInfo(int rownumb,int expId, String tableName, String columnName,String formatInfo) {
super();
this.rownumb=rownumb;
this.expId = expId;
this.tableName = tableName;
this.columnName=columnName;
this.formatInfo = formatInfo;
}
@Override
public String toString(){
return "insert into t_export(export_id,owner,table_name,col_name,format) values('"
+this.expId+"','SCOTT','"
+this.tableName+"','"
+this.columnName+"','"
+this.formatInfo+"');";
}
@Override
public int compareTo(RowInfo row) {//重写排序方法
// TODO Auto-generated method stub
return this.rownumb - row.rownumb;
} public int getExpId() {
return expId;
}
public void setExpId(int expId) {
this.expId = expId;
}
public String getTableName() {
return tableName;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
public String getFormatInfo() {
return formatInfo;
}
public void setFormatInfo(String formatInfo) {
this.formatInfo = formatInfo;
}
public int getRownumb() {
return rownumb;
}
public void setRownumb(int rownumb) {
this.rownumb = rownumb;
}
public String getColumnName() {
return columnName;
}
public void setColumnName(String columnName) {
this.columnName = columnName;
} }

ShowExcel.java

package pu;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap; import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row; public class ShowExcel {
public static void showExcelSheet(Map<Integer,ArrayList> map) throws Exception {//处理方法
HashMap hashmap = new HashMap();
     //放入需求的表名
hashmap.put("T_A","101");
hashmap.put("T_D","104");
hashmap.put("T_G","107");
        hashmap.put("T_K","111"); HSSFWorkbook workbook=new HSSFWorkbook(new FileInputStream(new File("E:\\data\\Dictionary.xls")));
HSSFSheet sheet=null;
FileOutputStream fs = new FileOutputStream(new File("E:\\output\\temp.txt"),false);//每次都覆盖
PrintStream p = new PrintStream(fs);
p.println("sheet amount==="+workbook.getNumberOfSheets()); for (int i = 0; i < workbook.getNumberOfSheets(); i++) {//获取每个Sheet表
sheet=workbook.getSheetAt(i);
ArrayList<RowInfo> list_rows = new ArrayList<>();//每个sheet需new一个list if (hashmap.containsKey(sheet.getSheetName()))
{
p.println("reading the sheet "+sheet.getSheetName());
for (int j = 0; j < sheet.getPhysicalNumberOfRows(); j++) {//获取每行
HSSFRow row=sheet.getRow(j);
if(row==null){//跳过空行
System.out.println("wowowo");
continue;
}
System.out.print("row amount=="+sheet.getPhysicalNumberOfRows()+" and now row"+j+"\t");
// p.print("table_name_"+sheet.getSheetName() +"\t"); if(j>2 && row.getCell(1)!=null){//此处跳过sheet中的前三行
p.print("table_name_"+sheet.getSheetName()+" row=="+j +"==\t");
p.print(hashmap.get(sheet.getSheetName()).toString() +"\t");
p.print(sheet.getSheetName() +"\t");
p.print(row.getCell(1).toString().toUpperCase()+"\t");
p.print(row.getCell(5)+"\t");
p.println();
            //装载每行的序号,字段名称,格式
list_rows.add(new RowInfo((int)row.getCell(0).getNumericCellValue(),Integer.parseInt(hashmap.get(sheet.getSheetName()).toString()),sheet.getSheetName().toString(),row.getCell(1).toString().toUpperCase().trim(),row.getCell(5).toString()));
}
for (int k = 0; k < row.getPhysicalNumberOfCells(); k++) {//获取每个单元格
if(row.getCell(k)!=null){
System.out.print(row.getCell(k)+"\t");
//p.print(row.getCell(k)+"\t");
}else
System.out.print("row.getCell(k) is nullnull"+"\t");
}
// p.println(" row over");
System.out.println("---Sheet表"+i+"处理完毕---");
}
p.println("---Sheet表"+i+"处理完毕---");
// 排序
Collections.sort(list_rows);
         
        //读完一张装入一张
map.put(Integer.parseInt(hashmap.get(sheet.getSheetName()).toString()), list_rows); }
}
p.close(); }
public static void main(String[] args) throws Exception{
FileOutputStream f = new FileOutputStream(new File("E:\\output\\result.txt"),false);
PrintStream rs = new PrintStream(f); try{
Map<Integer,ArrayList> map=new HashMap<>();
showExcelSheet(map);
Map<Integer,ArrayList> treemap=new TreeMap<>();//实现结果表对象按export_id排序 treemap.putAll(map);
for(int k:treemap.keySet()){
System.out.println(k);
}
for(ArrayList<RowInfo> tab :treemap.values()){
for(RowInfo row:tab){
rs.println(row.toString());
}
}
}catch(Exception e){e.printStackTrace();}
} }

java poi处理excel多sheet并实现排序的更多相关文章

  1. 重构:以Java POI 导出EXCEL为例

    重构 开头先抛出几个问题吧,这几个问题也是<重构:改善既有代码的设计>这本书第2章的问题. 什么是重构? 为什么要重构? 什么时候要重构? 接下来就从这几个问题出发,通过这几个问题来系统的 ...

  2. java POI创建Excel示例(xslx和xsl区别 )

    Java用来处理office类库有很多,其中POI就是比较出名的一个,它是apache的类库,现在版本到了3.10,也就是2014年2月8号这个版本. 在处理PPT,Excel和Word前,需要导入以 ...

  3. 在java poi导入Excel通用工具类示例详解

    转: 在java poi导入Excel通用工具类示例详解 更新时间:2017年09月10日 14:21:36   作者:daochuwenziyao   我要评论   这篇文章主要给大家介绍了关于在j ...

  4. JAVA POI替换EXCEL模板中自定义标签(XLSX版本)满足替换多个SHEET中自定义标签

    个人说明:为了简单实现导出数据较少的EXCEL(根据自定义书签模板) 一.替换Excel表格标签方法```/** * 替换Excel模板文件内容 * @param map * 需要替换的标签建筑队形式 ...

  5. java poi出excel换行问题

    POI操作excel实现换行问题. package jp.co.misumi.mdm.batch.common.jobrunner; import java.io.FileInputStream; i ...

  6. Java POI 导出EXCEL经典实现 Java导出Excel

    转自http://blog.csdn.net/evangel_z/article/details/7332535 在web开发中,有一个经典的功能,就是数据的导入导出.特别是数据的导出,在生产管理或者 ...

  7. java poi操作excel 添加 锁定单元格保护

    Excel的book保护是很常用的,主要是不想让别人修改Excel的时候用.这样能够避免恶意随便修改数据,提高数据的可信度. 下面介绍JAVA POI来实现设置book保护: 使用HSSFSheet类 ...

  8. Java POI 导出excel表

    1.首先下载poi-3.6-20091214.jar,下载地址如下: http://download.csdn.net/detail/evangel_z/3895051 2.Student.java ...

  9. Java POI读取Excel数据,将数据写入到Excel表格

    1.准备 首先需要导入poi相应的jar包,包括: 下载地址:http://pan.baidu.com/s/1bpoxdz5 所需要的包的所在位置包括: 2.读取Excel数据代码 package S ...

随机推荐

  1. C++进阶--隐式类型转换

    //############################################################################ /* 隐式类型转换 * * 类型转换可分为 ...

  2. apk重签名方法

    转载(http://www.51testing.com/?uid-115892-action-viewspace-itemid-223023) 1.      生成Android APK包签名证书 1 ...

  3. java 字节码 指令集 汇编(转)

    https://blog.csdn.net/github_35983163/article/details/52945845 网上找的没有指令码这列  自己把它加上 更方便查阅 指令从0x00-0xc ...

  4. java之基本数据类型

    11,java里面有没有long double类型或者比double更精度的? =========== 11,java里面有没有long double类型或者比double更精度的? java的基本数 ...

  5. Redis 在线管理工具(phpRedisAdmin)介绍 两次git

    phpRedisAdmin is a simple web interface to manage Redis databases. phpRedisAdmin 在 Redis clients 的列表 ...

  6. Linux常用指令之二

    1.用户权限     1).查看文件属性 ls -l file(ll别名)         drwxr-x--- 2 root root 4096 Jan 20 19:39 mnt         # ...

  7. 第8章 传输层(3)_TCP协议

    3. 传输控制协议(TCP) 3.1 TCP协议的主要特点 (1)TCP是面向连接的传输层协议.即使用TCP协议之前必须先建立TCP连接.在传送数据完毕之后,必须释放己经建立的TCP连接. (2)每一 ...

  8. unhandledException

    处理未捕获的异常是每个应用程序起码有的功能,C#在AppDomain提供了UnhandledException 事件来接收未捕获到的异常的通知.常见的应用如下: static void Main(st ...

  9. (转)C#实现注册码

    原文地址:http://www.cnblogs.com/netcorner/archive/2011/08/31/2911922.html 开发软件时,当用到商业用途时,注册码与激活码就显得很重要了. ...

  10. RabbitMQ manage

    1. RabbitMQ service sudo systemctl enable rabbitmq-server sudo systemctl start rabbitmq-server sudo ...