package com.cn.peitest.excel;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.text.DecimalFormat; import org.apache.poi.hssf.usermodel.HSSFCell;
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.poifs.filesystem.POIFSFileSystem; import com.cn.peitest.excel.tool.ExcelPoiTools; public class pei_testXLS {
/*
* 处理xls文件
* */ public static void main(String[] args){
try {
//生成111.txt文件的地址
PrintStream ps = new PrintStream("C:\\Users\\pei\\Desktop\\111.txt");
//设置将内容打印到111.txt文件中 //System.setOut(ps); //要打印的内容拼成sql文件
System.out.println("insert into `t_pub_bfshlp` values" );
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
HSSFWorkbook rwb = null;
HSSFSheet incomeSheet = null;
POIFSFileSystem fs = null;
HSSFRow row = null;
int l = 0;
//要读取的文件路径
String tableFile = "C:\\Users\\Pei\\Desktop\\HYD test 1_0_2 tm=2020_09_29_09_30.xls";
//读取excle时这句话是固定用法
fs = new POIFSFileSystem(new FileInputStream(new File(tableFile)));
rwb = new HSSFWorkbook(fs);
// 获取到Excel中的Sheet
incomeSheet = rwb.getSheetAt(1); StringBuffer date=new StringBuffer(""); for (int x = 5; x <= 314; x++) {//读取多少行 System.out.print("('99'," ); row = incomeSheet.getRow(x);
// 读取格 编号
for (int y = 0; y <= 4; y++) {//读取每行读多少列,获取前5列的值分别取前两位
l++;
HSSFCell cell = row.getCell(y);
if(cell!=null&&!ExcelPoiTools.getStringCellValue(cell).equals("")&&ExcelPoiTools.getStringCellValue(cell)!=null){ date =date.append( ExcelPoiTools.getStringCellValue(cell).substring(0,2));//获取每列值的前两位数 }
}
l=(date.length())/2; System.out.print("'PRD_IND_TYP"+""+"',"); System.out.print("'" + date+"',");
date.setLength(0); // 读取格 内容
for (int y = 5; y <= 5; y++) {//从第5列开始读取后面每列值得内容
HSSFCell cell = row.getCell(y);
if(cell!=null){
date =date.append( ExcelPoiTools.getStringCellValue(cell));
}
}
System.out.println("'" + date+"','','','20171110010101','','','','',''),");
date.setLength(0); } } catch (Exception e) {
System.out.println(e);
}
} //处理数据
public static String getStringCellValue(HSSFCell cell) {
String strCell = "";
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_STRING://字符串
strCell = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_NUMERIC:
DecimalFormat df = new DecimalFormat("0.00");//处理数字
strCell = df.format(cell.getNumericCellValue());
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
strCell = String.valueOf(cell.getBooleanCellValue());
break;
case HSSFCell.CELL_TYPE_BLANK://空值
strCell = "";
break;
default:
strCell = "";
break;
}
if ("".equals(strCell) || strCell == null) {
return "";
}
if (cell == null) {
return "";
}
return strCell;
}
}

  

//pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.com</groupId>
<artifactId>excelReadAndWrite</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.directory.studio</groupId>
<artifactId>org.apache.commons.codec</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.12</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
</dependencies>
</project>

  

最新文章

  1. 如何在EF中实现left join(左联接)查询
  2. php图片防盗链的小测试
  3. Python与Hack之Unix口令
  4. StackExchange.Redis.Extensions.Core 源码解读之 Configuration用法
  5. IE11之F12 Developer Tools--概述篇
  6. FROM_UNIXTIME 格式化MYSQL时间戳函数
  7. Scheme Implementations对比
  8. css文本超出2行就隐藏并显示省略号
  9. Redis操作命令大全(NodeJS版)
  10. css重置
  11. 蜗牛—苍茫IT文章大学的路(十)
  12. Asterisk 未来之路3.0_0002
  13. FFMPEG结构体分析:AVCodecContext
  14. MFC 读写文件
  15. Android为TV端助力 同时setTag两次,保存多种值
  16. 基于【磁盘】操作的IO接口:File
  17. 自学Python1.8-python input/print用法 格式化输出
  18. Redis入门到高可用(四)—— Redis的五种数据结构的内部编码
  19. Android 最基础生命周期及旋转屏幕问题
  20. Java堆内存设置

热门文章

  1. 区块链V1版本实现之四
  2. Python超全干货:【二叉树】基础知识大全
  3. 微软最强 Python 自动化工具开源了!不用写一行代码!
  4. MySQL错误日志(Error Log)
  5. C++编程指南续
  6. mysql-installer-web-community和mysql-installer-communityl两个版本的区别
  7. PyQt(Python+Qt)学习随笔:QAbstractItemView的SelectionBehavior属性
  8. leetcode计划(二)——ps:复习面试题计划+锻炼计划
  9. Java对象操作工具
  10. 在DLL中使用对话框