1. package cn.com.qmhd.tools;
  2.  
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.util.ArrayList;
  8. import java.util.HashMap;
  9. import java.util.List;
  10.  
  11. import javax.servlet.http.HttpServletRequest;
  12.  
  13. import org.apache.poi.hssf.usermodel.HSSFCell;
  14. import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  15. import org.apache.poi.hssf.usermodel.HSSFFont;
  16. import org.apache.poi.hssf.usermodel.HSSFRow;
  17. import org.apache.poi.hssf.usermodel.HSSFSheet;
  18. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  19. import org.apache.poi.hssf.util.HSSFColor;
  20. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  21. import org.apache.poi.xssf.usermodel.XSSFCell;
  22. import org.apache.poi.xssf.usermodel.XSSFRow;
  23. import org.apache.poi.xssf.usermodel.XSSFSheet;
  24. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  25.  
  26. public class ExcelUtils {
  27.  
  28. public int readLine ( String filePath ){
  29. try {
  30. POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream( filePath ));
  31. HSSFWorkbook workbook = new HSSFWorkbook(fs);
  32. HSSFSheet sheet = workbook.getSheetAt(0);
  33. int rows = sheet.getLastRowNum()+1;
  34. return rows ;
  35. } catch (FileNotFoundException e) {
  36. ExceptionHeading.getException(this.getClass().getName(), e, "ExcelUtils:readLine:FileNotFound:error");
  37. return -1 ;
  38. } catch (IOException e) {
  39. ExceptionHeading.getException(this.getClass().getName(), e, "ExcelUtils:readLine:IO:error");
  40. return -1 ;
  41. }
  42. }
  43.  
  44. public int readLine2007 ( String filePath ){
  45. try {
  46.  
  47. @SuppressWarnings("deprecation")
  48. XSSFWorkbook xwb = new XSSFWorkbook(filePath);
  49. XSSFSheet sheet = xwb.getSheetAt(0);
  50. int rows = sheet.getLastRowNum()+1;
  51. return rows ;
  52. } catch (FileNotFoundException e) {
  53. ExceptionHeading.getException(this.getClass().getName(), e, "ExcelUtils:readLine:FileNotFound:error");
  54. return -1 ;
  55. } catch (IOException e) {
  56. ExceptionHeading.getException(this.getClass().getName(), e, "ExcelUtils:readLine:IO:error");
  57. return -1 ;
  58. }
  59. }
  60.  
  61. public List< HashMap< String,String > > read( String fileName, HttpServletRequest request , int i , int line){
  62. List< HashMap< String,String > > list = new ArrayList< HashMap< String,String > >();
  63. try {
  64. POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream( request.getRealPath("/")+"upload/"+fileName ));
  65. HSSFWorkbook workbook = new HSSFWorkbook(fs);
  66. HSSFSheet sheet = workbook.getSheetAt(0);
  67. //int rows = sheet.getPhysicalNumberOfRows();
  68. int rows = (i+1)*1000;
  69. if ( rows >= line ) {
  70. rows = line;
  71. }
  72. for (int r = i*1000; r < rows; r++)
  73. {
  74. HSSFRow row = sheet.getRow(r);
  75. if ( row != null )
  76. {
  77. int cells = row.getLastCellNum();
  78. String value = "";
  79. HashMap< String,String > map = new HashMap< String,String >();
  80. for (short c = 0; c < cells; c++){
  81. HSSFCell cell = row.getCell(c);
  82. if (cell != null){
  83. switch (cell.getCellType())
  84. {
  85. case HSSFCell.CELL_TYPE_FORMULA:
  86. //
  87. break;
  88. case HSSFCell.CELL_TYPE_NUMERIC:
  89. value = (long) cell.getNumericCellValue()+"";
  90. break;
  91. case HSSFCell.CELL_TYPE_STRING:
  92. value = cell.getStringCellValue()+"";
  93. break;
  94. default:
  95. value = "";
  96. }
  97. map.put( c+"" , value );
  98. }
  99. }
  100. list.add( map );
  101. }
  102. }
  103. return list;
  104. } catch (FileNotFoundException e) {
  105. ExceptionHeading.getException(this.getClass().getName(), e, "ExcelUtils:read:FileNotFound:error");
  106. return list;
  107. } catch (IOException e) {
  108. ExceptionHeading.getException(this.getClass().getName(), e, "ExcelUtils:read:IO:error");
  109. return list;
  110. }
  111. }
  112.  
  113. public List<String[]> read2007( String filePath , int nTotal ) {
  114.  
  115. List<String[]> list = new ArrayList<String[]>();
  116. String[] strs = null;
  117.  
  118. try {
  119.  
  120. XSSFWorkbook xwb = new XSSFWorkbook(filePath);
  121. XSSFSheet sheet = xwb.getSheetAt(0);
  122. int rows =nTotal;
  123.  
  124. for (int r = 0; r < rows; r++){
  125.  
  126. XSSFRow row = sheet.getRow(r);
  127. if ( row != null && row.getLastCellNum()>0){
  128. int cells = row.getLastCellNum();
  129. String value = "";
  130. strs = new String[cells];
  131. for (short c = 0; c < cells; c++){
  132. XSSFCell cell = row.getCell(c);
  133. if (cell != null){
  134. switch (cell.getCellType())
  135. {
  136. case XSSFCell.CELL_TYPE_FORMULA:
  137. //
  138. break;
  139. case XSSFCell.CELL_TYPE_NUMERIC:
  140. value = (long) cell.getNumericCellValue()+"";
  141. break;
  142. case XSSFCell.CELL_TYPE_STRING:
  143. value = cell.getStringCellValue()+"";
  144. break;
  145. default:
  146. value = "";
  147. }
  148.  
  149. strs[ Integer.parseInt( c+"" ) ] = value;
  150. }
  151. }
  152. list.add( strs );
  153. }
  154. strs = null;
  155. }
  156. return list;
  157. } catch (FileNotFoundException e) {
  158. ExceptionHeading.getException(this.getClass().getName(), e, "ExcelUtils:read:FileNotFound:error");
  159. return null;
  160. } catch (IOException e) {
  161. ExceptionHeading.getException(this.getClass().getName(), e, "ExcelUtils:read:IO:error");
  162. return null;
  163. }
  164. }
  165.  
  166. public List<String[]> read( String filePath , int nTotal ) {
  167.  
  168. List<String[]> list = new ArrayList<String[]>();
  169. String[] strs = null;
  170.  
  171. try {
  172. POIFSFileSystem fs = new POIFSFileSystem( new FileInputStream( filePath ) );
  173. HSSFWorkbook workbook = new HSSFWorkbook(fs);
  174. HSSFSheet sheet = workbook.getSheetAt(0);
  175. int rows =nTotal;
  176.  
  177. for (int r = 0; r < rows; r++){
  178.  
  179. HSSFRow row = sheet.getRow(r);
  180. if ( row != null && row.getLastCellNum()>0){
  181. int cells = row.getLastCellNum();
  182. String value = "";
  183. strs = new String[cells];
  184. for (short c = 0; c < cells; c++){
  185. HSSFCell cell = row.getCell(c);
  186. if (cell != null){
  187. switch (cell.getCellType())
  188. {
  189. case HSSFCell.CELL_TYPE_FORMULA:
  190. //
  191. break;
  192. case HSSFCell.CELL_TYPE_NUMERIC:
  193. value = (long) cell.getNumericCellValue()+"";
  194. break;
  195. case HSSFCell.CELL_TYPE_STRING:
  196. value = cell.getStringCellValue()+"";
  197. break;
  198. default:
  199. value = "";
  200. }
  201.  
  202. strs[ Integer.parseInt( c+"" ) ] = value;
  203. }
  204. }
  205. list.add( strs );
  206. }
  207. strs = null;
  208. }
  209. return list;
  210. } catch (FileNotFoundException e) {
  211. ExceptionHeading.getException(this.getClass().getName(), e, "ExcelUtils:read:FileNotFound:error");
  212. return null;
  213. } catch (IOException e) {
  214. ExceptionHeading.getException(this.getClass().getName(), e, "ExcelUtils:read:IO:error");
  215. return null;
  216. }
  217. }
  218.  
  219. public boolean write( String filePath, List<String[]> list ) {
  220. // 新建文件
  221. HSSFWorkbook wb = new HSSFWorkbook();
  222. // 新建工作表
  223. HSSFSheet sheet = wb.createSheet("Sheet1");
  224. sheet.setDefaultColumnWidth((short) 20);
  225. sheet.setColumnWidth((short)7, (short)20000);
  226.  
  227. HSSFCellStyle style = wb.createCellStyle();
  228. style.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
  229. style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
  230. style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
  231. style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
  232. style.setBorderRight(HSSFCellStyle.BORDER_THIN);
  233. style.setBorderTop(HSSFCellStyle.BORDER_THIN);
  234. // 生成一个字体
  235. HSSFFont font = wb.createFont();
  236. //font.setColor(HSSFColor.VIOLET.index);
  237. font.setFontHeightInPoints((short) 12);
  238. font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
  239.  
  240. // 把字体应用到当前的样式
  241. style.setFont(font);
  242. for (int hang = 0; hang <list.size(); hang++)
  243. {
  244. // 创建行
  245. HSSFRow row = sheet.createRow((short) hang);
  246. String[] strs = list.get(hang);
  247. for (int lie = 0; lie < strs.length; lie++)
  248. {
  249.  
  250. HSSFCell cell = row.createCell((short) lie);// 创建格 createCell((short) lie)
  251.  
  252. if (hang == 0) {
  253. cell.setCellStyle(style);
  254. }
  255. cell.setCellValue(strs[lie]);
  256. }
  257. }
  258.  
  259. FileOutputStream fileout;
  260. try {
  261. fileout = new FileOutputStream(filePath);
  262. wb.write(fileout);
  263. fileout.close();
  264. return true;
  265. } catch (FileNotFoundException e) {
  266. ExceptionHeading.getException(this.getClass().getName(), e, "ExcelUtils:write:FileNotFound:error");
  267. } catch (IOException e) {
  268. ExceptionHeading.getException(this.getClass().getName(), e, "ExcelUtils:write:FileNotFound:error");
  269. }
  270.  
  271. return false;
  272. }
  273.  
  274. public boolean write2( String filePath, String[] strs ) {
  275.  
  276. // 新建文件
  277. HSSFWorkbook wb = new HSSFWorkbook();
  278. // 新建工作表
  279. HSSFSheet sheet = wb.createSheet("Sheet1");
  280. sheet.setDefaultColumnWidth((short) 20);
  281.  
  282. HSSFCellStyle style = wb.createCellStyle();
  283. style.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
  284. style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
  285. style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
  286. style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
  287. style.setBorderRight(HSSFCellStyle.BORDER_THIN);
  288. style.setBorderTop(HSSFCellStyle.BORDER_THIN);
  289. // 生成一个字体
  290. HSSFFont font = wb.createFont();
  291. //font.setColor(HSSFColor.VIOLET.index);
  292. font.setFontHeightInPoints((short) 12);
  293. font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
  294.  
  295. // 把字体应用到当前的样式
  296. style.setFont(font);
  297. for (int hang = 0; hang < 1; hang++)
  298. {
  299. HSSFRow row = sheet.createRow((short) hang);
  300. int nArg = strs.length;
  301. HSSFCell cell = row.createCell((short)(0));
  302. cell.setCellStyle(style);
  303. cell.setCellValue( "电话号码" );
  304. for (int lie = 0; lie < nArg; lie++)
  305. {
  306. // 创建格 createCell((short) lie)
  307. cell = row.createCell((short)(lie+1));
  308. cell.setCellStyle(style);
  309. cell.setCellValue( strs[lie] );
  310. }
  311. }
  312.  
  313. FileOutputStream fileout;
  314. try {
  315. fileout = new FileOutputStream(filePath);
  316. wb.write(fileout);
  317. fileout.close();
  318. return true;
  319. } catch (FileNotFoundException e) {
  320. ExceptionHeading.getException(this.getClass().getName(), e, "ExcelUtils:write2:FileNotFound:error");
  321. } catch (IOException e) {
  322. ExceptionHeading.getException(this.getClass().getName(), e, "ExcelUtils:write2:FileNotFound:error");
  323. }
  324.  
  325. return false;
  326. }
  327.  
  328. }

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. 数据库学习(整理)----6--Oracle如何快速备份和多次备份数表数据

    1.说明:  这里假设一种应用场景! 假设,银行系统中有大量的数据需要及时备份,如何才能快速高效呢! 条件需求: (1).不能设置同步锁(设置的会影响银行正常业务进行!使得银行系统处于维护状态,这是不 ...

  2. a*b(高进度乘以int类型的数)

    以下是我今日的a-b(高精度)的程序,\(^o^)/偶也偶也偶也偶也! 程序: #include<stdio.h> #include<string.h> char s[1000 ...

  3. DisUnity——Unity3D反编译资源提取利刃

    1.资源 软件及项目源码地址:https://github.com/ata4/disunity/releases 2.使用方法: 将待反编译的文件放入文件夹中:如:E:\Demo\ 在disunity ...

  4. Windows Phone 之文件下载进度和速度显示

    用http协议来下载网络上的文件,通常我们需要获取文件的下载进度和下载的速度来给用户等待过程的一个交代,那么在windows phone 7下可以使用WebClient类来实现这一功能,HttpWeb ...

  5. JavaScript学习心得(八)

    Cookie是Netscape发明的技术,是动态网站必不可少的部分,用于浏览器请求Web页面的超文本传输协议是一种无状态的协议. 两种方法维护状态:使用会话(session)(使用服务器技术实现,数据 ...

  6. swift官方文档中的switch中case let x where x.hasSuffix("pepper")是什么意思?

    在官方文档中,看到这句.但不明白什么意思. let vegetable = "red pepper" switch vegetable { case "celery&qu ...

  7. python 中调用windows系统api操作剪贴版

    # -*- coding: utf-8 -*- ''' Created on 2013-11-26 @author: Chengshaoling ''' import win32clipboard a ...

  8. 10055 - Hashmat the Brave Warrior

    Problem A Hashmat the brave warrior Input: standard input Output: standard output Hashmat is a brave ...

  9. bzoj 2631: tree 动态树+常数优化

    2631: tree Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 1716  Solved: 576[Submit][Status] Descrip ...

  10. 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 ...