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)) ...
随机推荐
- 数据库学习(整理)----6--Oracle如何快速备份和多次备份数表数据
1.说明: 这里假设一种应用场景! 假设,银行系统中有大量的数据需要及时备份,如何才能快速高效呢! 条件需求: (1).不能设置同步锁(设置的会影响银行正常业务进行!使得银行系统处于维护状态,这是不 ...
- a*b(高进度乘以int类型的数)
以下是我今日的a-b(高精度)的程序,\(^o^)/偶也偶也偶也偶也! 程序: #include<stdio.h> #include<string.h> char s[1000 ...
- DisUnity——Unity3D反编译资源提取利刃
1.资源 软件及项目源码地址:https://github.com/ata4/disunity/releases 2.使用方法: 将待反编译的文件放入文件夹中:如:E:\Demo\ 在disunity ...
- Windows Phone 之文件下载进度和速度显示
用http协议来下载网络上的文件,通常我们需要获取文件的下载进度和下载的速度来给用户等待过程的一个交代,那么在windows phone 7下可以使用WebClient类来实现这一功能,HttpWeb ...
- JavaScript学习心得(八)
Cookie是Netscape发明的技术,是动态网站必不可少的部分,用于浏览器请求Web页面的超文本传输协议是一种无状态的协议. 两种方法维护状态:使用会话(session)(使用服务器技术实现,数据 ...
- swift官方文档中的switch中case let x where x.hasSuffix("pepper")是什么意思?
在官方文档中,看到这句.但不明白什么意思. let vegetable = "red pepper" switch vegetable { case "celery&qu ...
- python 中调用windows系统api操作剪贴版
# -*- coding: utf-8 -*- ''' Created on 2013-11-26 @author: Chengshaoling ''' import win32clipboard a ...
- 10055 - Hashmat the Brave Warrior
Problem A Hashmat the brave warrior Input: standard input Output: standard output Hashmat is a brave ...
- bzoj 2631: tree 动态树+常数优化
2631: tree Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 1716 Solved: 576[Submit][Status] Descrip ...
- Codeforces 713 C Sonya and Problem Wihtout a Legend
Description Sonya was unable to think of a story for this problem, so here comes the formal descript ...