package cn.com.qmhd.tools;

 import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import javax.servlet.http.HttpServletRequest; import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
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.hssf.util.HSSFColor;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ExcelUtils { public int readLine ( String filePath ){
try {
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream( filePath ));
HSSFWorkbook workbook = new HSSFWorkbook(fs);
HSSFSheet sheet = workbook.getSheetAt(0);
int rows = sheet.getLastRowNum()+1;
return rows ;
} catch (FileNotFoundException e) {
ExceptionHeading.getException(this.getClass().getName(), e, "ExcelUtils:readLine:FileNotFound:error");
return -1 ;
} catch (IOException e) {
ExceptionHeading.getException(this.getClass().getName(), e, "ExcelUtils:readLine:IO:error");
return -1 ;
}
} public int readLine2007 ( String filePath ){
try { @SuppressWarnings("deprecation")
XSSFWorkbook xwb = new XSSFWorkbook(filePath);
XSSFSheet sheet = xwb.getSheetAt(0);
int rows = sheet.getLastRowNum()+1;
return rows ;
} catch (FileNotFoundException e) {
ExceptionHeading.getException(this.getClass().getName(), e, "ExcelUtils:readLine:FileNotFound:error");
return -1 ;
} catch (IOException e) {
ExceptionHeading.getException(this.getClass().getName(), e, "ExcelUtils:readLine:IO:error");
return -1 ;
}
} public List< HashMap< String,String > > read( String fileName, HttpServletRequest request , int i , int line){
List< HashMap< String,String > > list = new ArrayList< HashMap< String,String > >();
try {
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream( request.getRealPath("/")+"upload/"+fileName ));
HSSFWorkbook workbook = new HSSFWorkbook(fs);
HSSFSheet sheet = workbook.getSheetAt(0);
//int rows = sheet.getPhysicalNumberOfRows();
int rows = (i+1)*1000;
if ( rows >= line ) {
rows = line;
}
for (int r = i*1000; r < rows; r++)
{
HSSFRow row = sheet.getRow(r);
if ( row != null )
{
int cells = row.getLastCellNum();
String value = "";
HashMap< String,String > map = new HashMap< String,String >();
for (short c = 0; c < cells; c++){
HSSFCell cell = row.getCell(c);
if (cell != null){
switch (cell.getCellType())
{
case HSSFCell.CELL_TYPE_FORMULA:
//
break;
case HSSFCell.CELL_TYPE_NUMERIC:
value = (long) cell.getNumericCellValue()+"";
break;
case HSSFCell.CELL_TYPE_STRING:
value = cell.getStringCellValue()+"";
break;
default:
value = "";
}
map.put( c+"" , value );
}
}
list.add( map );
}
}
return list;
} catch (FileNotFoundException e) {
ExceptionHeading.getException(this.getClass().getName(), e, "ExcelUtils:read:FileNotFound:error");
return list;
} catch (IOException e) {
ExceptionHeading.getException(this.getClass().getName(), e, "ExcelUtils:read:IO:error");
return list;
}
} public List<String[]> read2007( String filePath , int nTotal ) { List<String[]> list = new ArrayList<String[]>();
String[] strs = null; try { XSSFWorkbook xwb = new XSSFWorkbook(filePath);
XSSFSheet sheet = xwb.getSheetAt(0);
int rows =nTotal; for (int r = 0; r < rows; r++){ XSSFRow row = sheet.getRow(r);
if ( row != null && row.getLastCellNum()>0){
int cells = row.getLastCellNum();
String value = "";
strs = new String[cells];
for (short c = 0; c < cells; c++){
XSSFCell cell = row.getCell(c);
if (cell != null){
switch (cell.getCellType())
{
case XSSFCell.CELL_TYPE_FORMULA:
//
break;
case XSSFCell.CELL_TYPE_NUMERIC:
value = (long) cell.getNumericCellValue()+"";
break;
case XSSFCell.CELL_TYPE_STRING:
value = cell.getStringCellValue()+"";
break;
default:
value = "";
} strs[ Integer.parseInt( c+"" ) ] = value;
}
}
list.add( strs );
}
strs = null;
}
return list;
} catch (FileNotFoundException e) {
ExceptionHeading.getException(this.getClass().getName(), e, "ExcelUtils:read:FileNotFound:error");
return null;
} catch (IOException e) {
ExceptionHeading.getException(this.getClass().getName(), e, "ExcelUtils:read:IO:error");
return null;
}
} public List<String[]> read( String filePath , int nTotal ) { List<String[]> list = new ArrayList<String[]>();
String[] strs = null; try {
POIFSFileSystem fs = new POIFSFileSystem( new FileInputStream( filePath ) );
HSSFWorkbook workbook = new HSSFWorkbook(fs);
HSSFSheet sheet = workbook.getSheetAt(0);
int rows =nTotal; for (int r = 0; r < rows; r++){ HSSFRow row = sheet.getRow(r);
if ( row != null && row.getLastCellNum()>0){
int cells = row.getLastCellNum();
String value = "";
strs = new String[cells];
for (short c = 0; c < cells; c++){
HSSFCell cell = row.getCell(c);
if (cell != null){
switch (cell.getCellType())
{
case HSSFCell.CELL_TYPE_FORMULA:
//
break;
case HSSFCell.CELL_TYPE_NUMERIC:
value = (long) cell.getNumericCellValue()+"";
break;
case HSSFCell.CELL_TYPE_STRING:
value = cell.getStringCellValue()+"";
break;
default:
value = "";
} strs[ Integer.parseInt( c+"" ) ] = value;
}
}
list.add( strs );
}
strs = null;
}
return list;
} catch (FileNotFoundException e) {
ExceptionHeading.getException(this.getClass().getName(), e, "ExcelUtils:read:FileNotFound:error");
return null;
} catch (IOException e) {
ExceptionHeading.getException(this.getClass().getName(), e, "ExcelUtils:read:IO:error");
return null;
}
} public boolean write( String filePath, List<String[]> list ) {
// 新建文件
HSSFWorkbook wb = new HSSFWorkbook();
// 新建工作表
HSSFSheet sheet = wb.createSheet("Sheet1");
sheet.setDefaultColumnWidth((short) 20);
sheet.setColumnWidth((short)7, (short)20000); HSSFCellStyle style = wb.createCellStyle();
style.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
// 生成一个字体
HSSFFont font = wb.createFont();
//font.setColor(HSSFColor.VIOLET.index);
font.setFontHeightInPoints((short) 12);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 把字体应用到当前的样式
style.setFont(font);
for (int hang = 0; hang <list.size(); hang++)
{
// 创建行
HSSFRow row = sheet.createRow((short) hang);
String[] strs = list.get(hang);
for (int lie = 0; lie < strs.length; lie++)
{ HSSFCell cell = row.createCell((short) lie);// 创建格 createCell((short) lie) if (hang == 0) {
cell.setCellStyle(style);
}
cell.setCellValue(strs[lie]);
}
} FileOutputStream fileout;
try {
fileout = new FileOutputStream(filePath);
wb.write(fileout);
fileout.close();
return true;
} catch (FileNotFoundException e) {
ExceptionHeading.getException(this.getClass().getName(), e, "ExcelUtils:write:FileNotFound:error");
} catch (IOException e) {
ExceptionHeading.getException(this.getClass().getName(), e, "ExcelUtils:write:FileNotFound:error");
} return false;
} public boolean write2( String filePath, String[] strs ) { // 新建文件
HSSFWorkbook wb = new HSSFWorkbook();
// 新建工作表
HSSFSheet sheet = wb.createSheet("Sheet1");
sheet.setDefaultColumnWidth((short) 20); HSSFCellStyle style = wb.createCellStyle();
style.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
// 生成一个字体
HSSFFont font = wb.createFont();
//font.setColor(HSSFColor.VIOLET.index);
font.setFontHeightInPoints((short) 12);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 把字体应用到当前的样式
style.setFont(font);
for (int hang = 0; hang < 1; hang++)
{
HSSFRow row = sheet.createRow((short) hang);
int nArg = strs.length;
HSSFCell cell = row.createCell((short)(0));
cell.setCellStyle(style);
cell.setCellValue( "电话号码" );
for (int lie = 0; lie < nArg; lie++)
{
// 创建格 createCell((short) lie)
cell = row.createCell((short)(lie+1));
cell.setCellStyle(style);
cell.setCellValue( strs[lie] );
}
} FileOutputStream fileout;
try {
fileout = new FileOutputStream(filePath);
wb.write(fileout);
fileout.close();
return true;
} catch (FileNotFoundException e) {
ExceptionHeading.getException(this.getClass().getName(), e, "ExcelUtils:write2:FileNotFound:error");
} catch (IOException e) {
ExceptionHeading.getException(this.getClass().getName(), e, "ExcelUtils:write2:FileNotFound:error");
} return false;
} }

java_method_readFile读取文件文本xls的更多相关文章

  1. java_method_readFile读取文件文本txt

    /** * @Title: TxtAndCsvUtils.java * @Package cn.com.qmhd.tools * @Description: TODO(读取txt和CSV文档) * @ ...

  2. C#读取固定文本格式的txt文件

    C#读取固定文本格式的txt文件 一个简单的C#读取txt文档的程序,文档中用固定的格式存放着实例数据. //判断关键字在文档中是否存在 ] == "设备ID:107157061" ...

  3. Springboot/SpringMvc 读取上传 xls 文件内容

    /** * 读取上传 xls 内容返回 * @param file * @return */@RequestMapping(value = "/read.xls")@Respons ...

  4. js进阶ajax读取json数据(ajax读取json和读取普通文本,和获取服务器返回数据(链接)都是一样的,在url处放上json文件的地址即可)

    js进阶ajax读取json数据(ajax读取json和读取普通文本,和获取服务器返回数据(链接)都是一样的,在url处放上json文件的地址即可) 一.总结 ajax读取json和读取普通文本,和获 ...

  5. php 读取网页源码 , 导出成txt文件, 读取xls,读取文件夹下的所有文件的文件名

    <?php // 读取网页源码$curl = curl_init();curl_setopt($curl, CURLOPT_URL, $url);curl_setopt($curl, CURLO ...

  6. java读取文件之txt文本

    1.方法一: public static String txt2String(File file){ 13 String result = ""; 14 try{ 15 Buffe ...

  7. POI-处理大Excel文件(xls)

    最近需要处理一个比较大的excel文件,但是poi在处理文件时会抛出OOM导致程序崩溃,查看官方文档看到可以以流式的方式读取excel避免读取大文件时的OOM.本文主要记述xls的处理. 环境模拟 先 ...

  8. phpspreadsheet 中文文档(六)读写文件+读取文件

    2019年10月11日14:05:58 读写文件 从体系结构您已经知道,使用基本PhpSpreadsheet类无法对持久性存储进行读写.为此,PhpSpreadsheet提供读者和作家,这是实现\Ph ...

  9. R语言--读取文件(数据输入)

    1 数据的输入 1.1 键盘输入 首先新建一张空表: dat<-data.frame(age=numeric(0),gender=character(0),weight=numeric(0)) ...

随机推荐

  1. json对象的处理

    不同的服务器接受的json对象要处理 var str1 = '{"name": "Lucy", "age": 23}'; var str2 ...

  2. 平衡搜索树(三) B-Tree

    B树的简介 B 树是为了磁盘或其它存储设备而设计的一种多叉平衡查找树.与红黑树很相似,但在降低磁盘I/0操作方面要更好一些(树的深度较低).许多数据库系统都一般使用B树或者B树的各种变形结构.B树与红 ...

  3. 【清橙A1084】【FFT】快速傅里叶变换

    问题描述 离散傅立叶变换在信号处理中扮演者重要的角色.利用傅立叶变换,可以实现信号在时域和频域之间的转换. 对于一个给定的长度为n=2m (m为整数) 的复数序列X0, X1, …, Xn-1,离散傅 ...

  4. php 连接mysql数据库并显示数据 实例 转载

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  5. Fedora 21 安装桌面环境

    Mate桌面环境:$ sudo yum install @mate-desktop KDE桌面环境:$ sudo yum install @kde-desktop XFCE桌面环境:$ sudo yu ...

  6. js获取时间加多山天和时间戳转换成日期

    function huoqu(){    var data = $("#data").val();//获取的时间    var day = $('#day').val();//往后 ...

  7. mysql datestamp坑

    每次更改行数据,该行第一个datestamp如不赋值,会自动更新为当前时间.赋值还要注意用下new Date(time).updated_at要写在created_at前面...

  8. [网络编程] TCP、UDP区别以及TCP传输原理、拥塞避免、连接建立、连接释放总结

    TCP.UDP都是属于运输层的协议,提供端到端的进程之间的逻辑通信,而IP协议(网络层)是提供主机间的逻辑通信,应用层规定应用进程在通信时所遵循的协议.一.UDP主要特点:传输的是用户数据报协议.1. ...

  9. 【python】python程序分行写符号

    方法1,用括号 >>> 3+4+(5+ 6)18 方法2 用 \ >>> 3+5+\ 412

  10. Python学习 - 编写自己的ORM(2)

    上一篇文章简单的实现了ORM(对象关系模型),这一篇文章主要实现简单的MySQL数据库操作. 想要操作数据库,首先要建立一个数据库连接.下面定义一个创建数据库连接的函数,得到一个连接叫做engine. ...