package dat.datadeal;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Date;

import java.util.List;

import java.util.Locale;

import java.util.logging.Level;

import java.util.logging.Logger;

import org.apache.poi.hssf.usermodel.HSSFCell;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;

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.ss.usermodel.Cell;

import org.apache.poi.ss.usermodel.DateUtil;

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.XSSFWorkbook;

/**

*

* @author daT dev.tao@gmail.com

*2003,2007版excel读写工具

*/

public class ExcelUtil{

/**

* Excel文件读取

* @param filePath

* @return String[]存的是行,List存的是列。

* 一个excel一次全部读入内存(Excel超大需要另行处理)

*/

public  List readExcel(String filePath) {

List dataList = new ArrayList();

boolean isExcel2003 = true;

if (isExcel2007(filePath)) {

isExcel2003 = false;

}

File file = new File(filePath);

InputStream is = null;

try {

is = new FileInputStream(file);

} catch (FileNotFoundException ex) {

Logger.getLogger(ExcelUtil.class.getName()).log(Level.SEVERE, null, ex);

}

Workbook wb = null;

try {

wb = isExcel2003 ? new HSSFWorkbook(is) : new XSSFWorkbook(is);

} catch (IOException ex) {

Logger.getLogger(ExcelUtil.class.getName()).log(Level.SEVERE, null, ex);

}

Sheet sheet = wb.getSheetAt(0);

int totalRows = sheet.getPhysicalNumberOfRows();

int totalCells = 0;

if (totalRows >= 1 && sheet.getRow(0) != null) {

totalCells = sheet.getRow(0).getPhysicalNumberOfCells();

}

for (int r = 0; r 

Row row = sheet.getRow(r);

if (row == null) {

continue;

}

String[] rowList = new String[totalCells];

for (int c = 0; c 

Cell cell = row.getCell(c);

String cellValue = "";

if (cell == null) {

rowList[c] = (cellValue);

continue;

}

cellValue = ConvertCellStr(cell, cellValue);

rowList[c] = (cellValue);

}

dataList.add(rowList);

}

return dataList;

}

private String ConvertCellStr(Cell cell, String cellStr) {

switch (cell.getCellType()) {

case Cell.CELL_TYPE_STRING:

// 读取String

cellStr = cell.getStringCellValue().toString();

break;

case Cell.CELL_TYPE_BOOLEAN:

// 得到Boolean对象的方法

cellStr = String.valueOf(cell.getBooleanCellValue());

break;

case Cell.CELL_TYPE_NUMERIC:

// 先看是否是日期格式

if (DateUtil.isCellDateFormatted(cell)) {

// 读取日期格式

cellStr = formatTime(cell.getDateCellValue().toString());

} else {

// 读取数字

cellStr = String.valueOf(cell.getNumericCellValue());

}

break;

case Cell.CELL_TYPE_FORMULA:

// 读取公式

cellStr = cell.getCellFormula().toString();

break;

}

return cellStr;

}

private boolean isExcel2007(String fileName) {

return fileName.matches("^.+\\.(?i)(xlsx)$");

}

private String formatTime(String s) {

SimpleDateFormat sf = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy", Locale.ENGLISH);

Date date = null;

try {

date = sf.parse(s);

} catch (ParseException ex) {

Logger.getLogger(ExcelUtil.class.getName()).log(Level.SEVERE, null, ex);

}

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

String result = sdf.format(date);

return result;

}

/**

* Excel写操作,简单起见还是采用内存数据一次写入

* @param filePath 输出文件路径名

* @param dataList 输出文件内容,List行  List列

* @throws IOException

*/

public  void writeExcel(String filePath,List> dataList) throws IOException{

HSSFWorkbook wb = new HSSFWorkbook();

HSSFSheet sheet = wb.createSheet("sheet");// 添加sheet

// 表格样式

HSSFCellStyle style = wb.createCellStyle();

style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 指定单元格居中对齐

// // 边框

// style.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);

// style.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);

// style.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);

// style.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);

// //设置字体

// HSSFFont f = wb.createFont();

// f.setFontHeightInPoints((short)10);

// f.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);

// style.setFont(f);

// //设置列宽

// sheet.setColumnWidth((short)0, (short)9600);

// sheet.setColumnWidth((short)1, (short)4000);

// sheet.setColumnWidth((short)2, (short)8000);

// sheet.setColumnWidth((short)3, (short)8000);

// 在索引0的位置创建第一行

for (int i = 0; i 

HSSFRow row = sheet.createRow(i);

List list = dataList.get(i);

for (int j = 0; j 

HSSFCell cell = row.createCell(j);

cell.setCellValue(list.get(j));

cell.setCellStyle(style);

}

}

// 导出文件

FileOutputStream fout = new FileOutputStream(filePath);

wb.write(fout);

fout.close();

}

}

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐