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)) ...
随机推荐
- Python:字典
#!/usr/bin/python3 #dict 字典 #字典是可变的 dict1 = {"name":"张三","age":22} pri ...
- 在Windows下用MingW 4.5.2编译OpenCV 2.3.0
需要的工具:1.安装QT SDK环境2.安装CMake for Windows3.OpenCV最新Windows源码步骤:1.将QT SDK安装目录下的{QtSDK}\mingw\bin添加到系统环境 ...
- ACM YTU 挑战编程 字符串 Problem A: WERTYU
Problem A: WERTYU Description A common typing error is to place yourhands on the keyboard one row to ...
- Excel里面将头尾第一个字母保留,去除中间的用*号代替,主要是REPT函数的应用,一开始我还以为要自己写个自定义函数
Excel里面将头尾第一个字母保留,去除中间的用*号代替,主要是REPT函数的应用,一开始我还以为要自己写个自定义函数 =LEFT(A1,1)&REPT("*",(LEN( ...
- DOM中的范围 createRange()
学习<JavaScript 高级程序设计> 12章dom范围的笔记 dom2级在Document类型中定义了 createRange()方法: 创建range对象很简单 var range ...
- 【pyhton】import math与import cmath
import math与import cmath分别代表导入math模块和复数math模块 还有一种导入方式是 from math import sqrt 从math中单独导入sqrt 直接可以用sq ...
- ibatis错误
java.lang.IllegalArgumentException: Mapped Statement collection already contains value for com.regin ...
- js将数字转换成大写的人民币表达式
function changeNumMoneyToChinese(money) { var cnNums = new Array("零", "壹", " ...
- Java反射的理解
反射的作用: 1.运行时检查类的结构 2.运行时更改类的字段值 3.调用类的方法 准备知识: Class类:虚拟机为每一个对象保存的一份对象所属类的清单: static Class for ...
- 【UVA10537】The Toll! Revisited (逆推最短路)
题目: Sample Input1a Z19 a Z5A DD XA bb cc X39 A X-1Sample OutputCase 1:20a-ZCase 2:44A-b-c-X 题意: 有两种节 ...