java_method_readFile读取文件文本xls
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的更多相关文章
- java_method_readFile读取文件文本txt
/** * @Title: TxtAndCsvUtils.java * @Package cn.com.qmhd.tools * @Description: TODO(读取txt和CSV文档) * @ ...
- C#读取固定文本格式的txt文件
C#读取固定文本格式的txt文件 一个简单的C#读取txt文档的程序,文档中用固定的格式存放着实例数据. //判断关键字在文档中是否存在 ] == "设备ID:107157061" ...
- Springboot/SpringMvc 读取上传 xls 文件内容
/** * 读取上传 xls 内容返回 * @param file * @return */@RequestMapping(value = "/read.xls")@Respons ...
- js进阶ajax读取json数据(ajax读取json和读取普通文本,和获取服务器返回数据(链接)都是一样的,在url处放上json文件的地址即可)
js进阶ajax读取json数据(ajax读取json和读取普通文本,和获取服务器返回数据(链接)都是一样的,在url处放上json文件的地址即可) 一.总结 ajax读取json和读取普通文本,和获 ...
- php 读取网页源码 , 导出成txt文件, 读取xls,读取文件夹下的所有文件的文件名
<?php // 读取网页源码$curl = curl_init();curl_setopt($curl, CURLOPT_URL, $url);curl_setopt($curl, CURLO ...
- java读取文件之txt文本
1.方法一: public static String txt2String(File file){ 13 String result = ""; 14 try{ 15 Buffe ...
- POI-处理大Excel文件(xls)
最近需要处理一个比较大的excel文件,但是poi在处理文件时会抛出OOM导致程序崩溃,查看官方文档看到可以以流式的方式读取excel避免读取大文件时的OOM.本文主要记述xls的处理. 环境模拟 先 ...
- phpspreadsheet 中文文档(六)读写文件+读取文件
2019年10月11日14:05:58 读写文件 从体系结构您已经知道,使用基本PhpSpreadsheet类无法对持久性存储进行读写.为此,PhpSpreadsheet提供读者和作家,这是实现\Ph ...
- R语言--读取文件(数据输入)
1 数据的输入 1.1 键盘输入 首先新建一张空表: dat<-data.frame(age=numeric(0),gender=character(0),weight=numeric(0)) ...
随机推荐
- (正则表达式应用) 替换自闭合标签(self-closing tag)的method
var str = "<sup><div class=\"he's\"/></sup><span id=\"cs\&q ...
- php 单引号与双引号区别
一.单引号与双引号区别 1." "双引号里面的字段会经过编译器解释,然后再当作HTML代码输出. 2.' '单引号里面的不进行解释,直接输出. 从字面意思上就可以看出,单引号比双引 ...
- ubuntu14.04 开启root登陆
想要在登录界面使用root身份登录,可编辑/usr/share/lightdm/lightdm.conf.d/50-ubuntu.conf文件, sudo gedit /usr/share/light ...
- python运维开发之第二天
一.模块初识: 1.模块定义 python是由一系列的模块组成的,每个模块就是一个py为后缀的文件,同时模块也是一个命名空间,从而避免了变量名称冲突的问题.模块我们就可以理解为lib库,如果需要使用某 ...
- 工欲善其事必先利其器-Notepad++使用小记(Python)
大学开始就一直使用Notepad++ 作为代码编辑器,喜欢它的简洁明了,喜欢它的个性,也喜欢它各种各样骚气的插件. 今天闲来无事,写篇文章记录一下平时使用的种种,包括但不限于个性化使用一些宏,快捷键, ...
- u-boot Makefile Source Test
一.概述 笔者已经写了一篇实现目标文件与源码分开的makefile测试实验,但是觉得不够完美,没有更多的体现u-boot Makefile的工作原理和特点.所以,决定重新修订,使之更加充分的接近u-b ...
- PC硬件之我见——CPU篇
写在最前面: 最近身边很多朋友都购置电脑的想法,往往也会选择性价比较高的DIY攒机方式.不幸的是,并不是所有人都对电脑硬件有一定的了解的,何况在现在这种社会风气下,盲目的相信商家是不明智的.所 ...
- [BZOJ - 2631] tree 【LCT】
题目链接:BZOJ - 2631 题目分析 LCT,像线段树区间乘,区间加那样打标记. 这道题我调了一下午. 提交之后TLE了,我一直以为是写错了导致了死循环. 于是一直在排查错误.直到.. 直到我看 ...
- Early 80386 CPUs
Assembling a detailed and accurate history of the 80386, including a complete listing of all the &qu ...
- 国内更新Android SDK汇总
以下两个网站提供了响应的办法. http://www.androiddevtools.cn/ --国内镜像 http://blog.csdn.net/boonya/article/details/38 ...