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. Frameset布局

    <FRAMESET> <FRAME> <NOFRAMES> <IFRAME> ■ 框架概念 : 所谓框架便是网页画面分成几个框窗,同时取得多个 URL. ...

  2. Windowsphone 之xml序列化和反序列化的应用(WebService解析返回的数据DataSet )

    关于Xml的序列化和反序列化: 可以看这篇文章,http://www.cnblogs.com/Windows-phone/p/3243575.html WebService解析返回的数据DataSet ...

  3. [转]python yield

    任何使用yield的函数都称之为生成器,如: def count(n): while n > 0: yield n   #生成值:n n -= 1 另外一种说法:生成器就是一个返回迭代器的函数, ...

  4. 关于Html无宽度居中

    代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title ...

  5. H5非主体结构元素

    1.header元素:页面中一个内容区块或整个页面的标题: 具有引导和导航作用的结构元素,通常用来放置整个页面或页面内的一个内容区块的标题,也可以包含数据表格.搜索表单或相关的logo图片. 一个网页 ...

  6. Unity NGUI和UGUI与模型、特效的层级关系

    目录 1.介绍两大UI插件NGUI和UGUI 2.unity渲染顺序控制方式 3.NGUI的控制 4.UGUI的控制 5.模型深度的控制 6.粒子特效深度控制 7.NGUI与模型和粒子特效穿插层级管理 ...

  7. OnClose()和 OnDestroy()

    OnClose()和 OnDestroy() 基于对话框的MFC程序,发现每次程序退出时,托盘的小图标不能自动消失,鼠标移上去之后才能消失,比较不爽. 后来发现我删除这个图标的代码是在自己重写的OnC ...

  8. 从一个模板函数聊聊模板函数里面如何获得T的名字

    写了个小程序,遇到点问题.总结总结,学习学习 #include<vector> #include<iostream> #include<typeinfo> usin ...

  9. [转]基于Spring + Spring MVC + Mybatis 高性能web构建

    http://blog.csdn.net/zoutongyuan/article/details/41379851/ 一直想写这篇文章,前段时间 痴迷于JavaScript.NodeJs.Angula ...

  10. 一个消除if语句的例子

    // 一个按钮点击事件,判断点击按钮是那一个显示出他的信息 - (IBAction)buttonPressed:(id)sender { if (sender == self.leftButton) ...