123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- package com.sinosoft.em.evaluate.excel;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Map;
- import javax.servlet.http.HttpServletRequest;
- import org.apache.commons.fileupload.FileItemIterator;
- import org.apache.commons.fileupload.FileItemStream;
- import org.apache.commons.fileupload.servlet.ServletFileUpload;
- import org.apache.poi.hssf.usermodel.HSSFCell;
- import org.apache.poi.hssf.usermodel.HSSFWorkbook;
- import org.apache.poi.ss.usermodel.Cell;
- import org.apache.poi.ss.usermodel.Row;
- import org.apache.poi.ss.usermodel.Sheet;
- import org.apache.poi.ss.usermodel.Workbook;
- import org.apache.poi.xssf.usermodel.XSSFCell;
- import org.apache.poi.xssf.usermodel.XSSFWorkbook;
- public class ExcelUitl {
-
- /**
- * 适用于没有标题行的excel
- * @throws Exception
- */
- public static List<List<String>> readExcelWithoutTitle(String filepath) throws Exception{
- String fileType = filepath.substring(filepath.lastIndexOf(".") + 1, filepath.length());
- InputStream is = null;
- Workbook wb = null;
- try {
- is = new FileInputStream(filepath);
-
- if (fileType.equals("xls")) {
- wb = new HSSFWorkbook(is);
- } else if (fileType.equals("xlsx")) {
- wb = new XSSFWorkbook(is);
- } else {
- throw new Exception("读取的不是excel文件");
- }
-
- List<List<String>> result = new ArrayList<List<String>>();//对应excel文件
-
- Sheet sheet = wb.getSheetAt(0);
-
- int rowSize = sheet.getLastRowNum() + 1;
- for (int i = 3; i < rowSize; i++) {//遍历行
- List<String> sheetList = new ArrayList<String>();//对应sheet页
- Row row = sheet.getRow(i);
- if (row == null) {//略过空行
- continue;
- }
- int cellSize = row.getLastCellNum();//行中有多少个单元格,也就是有多少列
- for(int j=0;j<cellSize;j++){
- Cell cell = row.getCell(j);
- String value = "";
- if(cell.getCellType()==Cell.CELL_TYPE_BOOLEAN){
- value = String.valueOf(cell.getBooleanCellValue());
- }else if(cell.getCellType()==Cell.CELL_TYPE_NUMERIC){
- value = String.valueOf(cell.getNumericCellValue());
- }else{
- value = String.valueOf(cell.getStringCellValue());
- }
- if(j>=14)
- sheetList.add(value);
- }
-
- result.add(sheetList);
- }
-
- return result;
- } catch (FileNotFoundException e) {
- throw e;
- } finally {
- if (wb != null) {
- wb.close();
- }
- if (is != null) {
- is.close();
- }
- }
- }
-
- @SuppressWarnings("static-access")
- private String getValue(XSSFCell xssfRow) {
- if (xssfRow.getCellType() == xssfRow.CELL_TYPE_BOOLEAN) {
- return String.valueOf(xssfRow.getBooleanCellValue());
- } else if (xssfRow.getCellType() == xssfRow.CELL_TYPE_NUMERIC) {
- return String.valueOf(xssfRow.getNumericCellValue());
- } else {
- return String.valueOf(xssfRow.getStringCellValue());
- }
- }
- @SuppressWarnings("static-access")
- private String getValue(HSSFCell hssfCell) {
- if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) {
- return String.valueOf(hssfCell.getBooleanCellValue());
- } else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) {
- return String.valueOf(hssfCell.getNumericCellValue());
- } else {
- return String.valueOf(hssfCell.getStringCellValue());
- }
- }
-
-
- public static void main(String[] args) {
- /*String filePath = "C:\\Users\\len\\Desktop\\excelDemo\\ceshi.xlsx";
- try {
- ExcelUitl.readExcelWithoutTitle(filePath);
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }*/
- System.out.println(Integer.parseInt("00.000"));
- }
-
-
- /**
- * 获取文件相信信息
- *
- * @param request
- * HttpServletRequest实例
- * @param imgPath
- * 图片路径
- * @param fileName
- * 图片名
- * @param path
- *
- * ftp保存的位置
- * @return
- * @throws IOException
- */
- public static Map getFileMessage(HttpServletRequest request,
- String imgPath, String fileName, String path) throws IOException {
- Map map = null;
- if (ServletFileUpload.isMultipartContent(request)) {
- // 创建ServletFileUpload实例
- ServletFileUpload fileUpload = new ServletFileUpload();
- FileItemIterator iter = null;
- FileItemStream item = null;
- InputStream is = null;
- try {
- // 解析request请求 返回FileItemStream的iterator实例
- iter = fileUpload.getItemIterator(request);
- // 迭代取出
- while (iter.hasNext()) {
- item = iter.next();// 获取文件流
- if (!item.isFormField()) {
- // 为什么要这么取出这个流呢,因为request.getInputStream()中,表单提交上来的不仅仅包含了文件,还带有参数,就算不带参数,也还有request中本身的一些其他东西(ps:没有研究过,但是试过不带参数拿到的流也是不对的。)直接拿会导致读取出来的文件变大,图片读取失败
- // 这里主要针对图片来写的,因为我用到的是转成图片,获取图片属性。
- is = item.openStream();
- if (is.available() > 0) {
-
- }
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- // return is;
- return map;
- } finally {
- fileUpload = null;
- iter = null;
- if (is != null) {
- is.close();
- is = null;
- }
- item = null;
- }
- }
- // return is;
- return map;
- }
-
- }
|