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)) ...
随机推荐
- 转: angularjs 指令中动态编译的方法(适用于有异步请求的情况) 内嵌指令无效
angular的坑很多 例子: 在directive的link中有一个$http请求,当请求完成后根据返回的值动态做element.append('......');这个操作, 能显示没问题,可问题是 ...
- 打印出不同顺序的字符串&单引号和双引号的差异
发现一个很好玩的打印顺序 package com.liaojianya.chapter1; /** * This program demonstrates the string. * @author ...
- Win8 +PHP+IIS配置
1.安装IIS:控制面板-程序和功能-打开或关闭Windows功能 2.配置PHP环境 -添加ISAPI筛选: -添加脚本映射:
- 用frame实现最基本的上中下三层布局,中间又分左右两部分.
用frame实现最基本的上中下三层布局,中间又分左右两部分. 用frame的好处在于不用象DIV一样要对浮动和大小进行精确控制,以及要考虑宽屏的时候怎么办.而且在导航的时候非常简单.比如说,左边是导航 ...
- go和swift
你生命中的有些东西终究会失去,比如我住了6年的陈寨,这个聚集了郑州十几万IT民工的地方,说拆就拆了.再比如我玩了3年的坦克英雄,这个带给我太多快乐的游戏,说停就停了. 编程对我而言是种爱好,我上学6年 ...
- 基于Lua的清除类游戏算法
最近在开发游戏,用Lua语言.习惯了其它的语言,然后对Lua的一些语法很不习惯. 比如table的元素个数的取值,比switch语句等等. 不过没有办法,还是要运用Lua来写游戏的.看来学C++还真的 ...
- Contest 20140923 潛行世界 拓撲排序,期望
潜行世界 查看 提交 统计 提问 总时间限制: 10000ms 内存限制: 256000kB 描述 HJA和学弟还在旅游中,这次他们来到了潜行世界.潜行世界是一个N个点M条边的有向无环图.每条路对 ...
- bzoj 1002 [FJOI2007]轮状病毒 高精度&&找规律&&基尔霍夫矩阵
1002: [FJOI2007]轮状病毒 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 2234 Solved: 1227[Submit][Statu ...
- Android 如何自定义EditText 下划线?
项目要求: 笔者曾经做过一个项目,其中登录界面的交互令人印象深刻.交互设计师给出了一个非常作的设计,要求做出包含根据情况可变色的下划线,左侧有可变图标,右侧有可变删除标志的输入框,如图 记录制作过程: ...
- 深入了解一下PYTHON中关于SOCKETSERVER的模块-B
请求多个文件的原型. 这个是最草的情况,就是硬编码到内存中的字符串, 真实的应用还是会转到其它端口处理,或是读到硬盘上的文件吧. #!/usr/bin/env python from BaseHTTP ...