package com.test.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
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.CellRangeAddress;
import org.apache.poi.hssf.util.Region;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
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 com.ucf.platform.framework.core.log.UcfLogger;
import com.ucf.platform.framework.core.log.UcfLoggerFactory;
@SuppressWarnings("all")
public class PoiCommonUtil {
private static final UcfLogger logger = UcfLoggerFactory.getLogger(PoiCommonUtil.class);
/**
* 日期类型转换
*/
private final static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
/**
* 将Excel数据导入到表中
*
* @param filePath
* Excel路径
* @param tableName
* 表名
*/
public static void readExcel(String filePath) throws Exception {
try {
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(
filePath));
// 创建工作簿
HSSFWorkbook workBook = new HSSFWorkbook(fs);
// 获得工作表的个数
int sheetNum = workBook.getNumberOfSheets();
logger.info("工作表个数 :" + sheetNum);
// 打印出工作表的名称
for (int i = 0; i < sheetNum; i++) {
logger.info("工作表名称:" + workBook.getSheetName(i));
}
// 循环工作表
for (int i = 0; i < sheetNum; i++) {
// Sheet(术语:工作表)就是Excel表格左下角的Sheet1,Sheet2,Sheet3但在程序中
// Sheet的下标是从0开始
HSSFSheet sheet = workBook.getSheetAt(i);
// 取出工作表中的可用的行数
int rows = sheet.getPhysicalNumberOfRows();
logger.info("取得的行数:" + rows);
if (rows > 0) {
double margin = sheet.getMargin(HSSFSheet.TopMargin);
logger.info("边距:" + margin);
// 行循环
for (int r = 0; r < rows; r++) {
HSSFRow row = sheet.getRow(r);
// 不取第一行
if (row != null && r != 0) {
// 获得本行中单元格的个数
int cells = row.getLastCellNum();
logger.info("本行中单元格的个数:" + cells);
// 获得总列数
int numbercell = row.getPhysicalNumberOfCells();
logger.info("numbercell:" + numbercell);
// 定义集合datas用于存Excel中一个行的数据
// Vector datas = new Vector();
// 列循环
for (short c = 0; c < cells; c++) {
HSSFCell cell = row.getCell(c);
// 不取第一列
if (cell != null && c != 0) {
String value = getValue(cell);
logger.info("第" + r + "行 " + "第" + c
+ "列:" + value);
// datas.add(value);
}
}
// 向表中插入数据
// DBFactory.insertData(dbConn, tableName, datas);
}
}
} else {
break;
}
}
} catch (Exception ex) {
ex.printStackTrace();
logger.info("readExcel 方法异常:" + ex);
throw ex;
}
}
/**
* 获取Excel中某个单元格的值
*
* @param cell
* @return
* @throws ParseException
*/
public static String getValue(HSSFCell cell) throws ParseException {
String value = "";
// Cell的6种类型
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_NUMERIC: // 数值型
if (HSSFDateUtil.isCellDateFormatted(cell)) {
// 如果是date类型则 ,获取该cell的date值
value = HSSFDateUtil.getJavaDate(cell.getNumericCellValue())
.toString();
java.util.Date date1 = new Date(value);
value = format.format(date1);
} else {// 纯数字
value = String.valueOf(cell.getNumericCellValue());
}
break;
/* 此行表示单元格的内容为string类型 */
case HSSFCell.CELL_TYPE_STRING: // 字符串型
value = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_FORMULA:// 公式型
// 读公式计算值
value = String.valueOf(cell.getNumericCellValue());
if (value.equals("NaN")) {// 如果获取的数据值为非法值,则转换为获取字符串
value = cell.getStringCellValue().toString();
}
cell.getCellFormula();
break;
case HSSFCell.CELL_TYPE_BOOLEAN:// 布尔
value = " " + cell.getBooleanCellValue();
break;
/* 此行表示该单元格值为空 */
case HSSFCell.CELL_TYPE_BLANK: // 空值
value = "";
break;
case HSSFCell.CELL_TYPE_ERROR: // 故障
value = "";
break;
default:
value = cell.getStringCellValue().toString();
}
return value;
}
/**
* 创建Excel
*
* @param path
*/
public static void createExcel(String path) {
try {
// 创建新的Excel 工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
// 在Excel工作簿中建一工作表,其名为缺省值
// 如要新建一名为"效益指标"的工作表,其语句为:
// HSSFSheet sheet = workbook.createSheet("效益指标");
HSSFSheet sheet = workbook.createSheet();
// 在索引0的位置创建行(最顶端的行)
HSSFRow row = sheet.createRow((short) 0);
// 在索引0的位置创建单元格(左上端)
HSSFCell cell = row.createCell((short) 0);
// 定义单元格为字符串类型
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
// 创建字体,设置其为红色、粗体:
HSSFFont font = workbook.createFont();
font.setColor(HSSFFont.COLOR_RED);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// 创建格式
HSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFont(font);
// 应用格式
// 在单元格中输入一些内容
cell.setCellStyle(cellStyle);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue("标题 ");
// 新建一输出文件流
FileOutputStream fOut = new FileOutputStream(path);
// 把相应的Excel 工作簿存盘
workbook.write(fOut);
fOut.flush();
// 操作结束,关闭文件
fOut.close();
logger.info("文件生成。。。。。。。。。。。。。。。。。。");
} catch (Exception e) {
logger.error("已运行 xlCreate() : " + e);
}
}
// 创建excel
public static void createSheet(String path) {
try {
HSSFWorkbook workBook = new HSSFWorkbook();// 创建 一个excel文档对象
HSSFSheet sheet = workBook.createSheet();// 创建一个工作薄对象
// 设置样式
HSSFCellStyle titleStyle = workBook.createCellStyle();// 创建样式对象
titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER_SELECTION);// 水平居中
titleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直居中
// 设置字体
HSSFFont titleFont = workBook.createFont();// 创建字体对象
titleFont.setFontHeightInPoints((short) 15);// 设置字体大小
titleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 设置粗体
titleFont.setFontName("黑体");// 设置为黑体字
titleStyle.setFont(titleFont);
// 合并单元格操作
sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, 5));
HSSFRow row = null;
HSSFCell cell = null;
row = sheet.createRow(0); // 第一行
cell = row.createCell(0);// 第一列
cell.setCellStyle(titleStyle);// 添加样式
cell.setCellValue(new HSSFRichTextString("学生信息表"));
// 设置表文样式
HSSFCellStyle tableStyle = workBook.createCellStyle();
tableStyle.setBorderBottom((short) 1);
tableStyle.setBorderTop((short) 1);
tableStyle.setBorderLeft((short) 1);
tableStyle.setBorderRight((short) 1);
tableStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 设置表文字体
HSSFFont tableFont = workBook.createFont();
tableFont.setFontHeightInPoints((short) 12); // 设置字体大小
tableFont.setFontName("宋体"); // 设置为黑体字
tableStyle.setFont(tableFont);
String[] title = { "编号", "学号", "姓名", "性别", "生日", "电话", "学校", "夕阳" };
row = sheet.createRow(2); // 创建第二行
for (int i = 0; i < title.length; i++) {
cell = row.createCell(i);
cell.setCellStyle(tableStyle);
cell.setCellValue(new HSSFRichTextString(title[i]));
}
/*
* for (int i = 0; i < list.size(); i++) {
*
* row = sheet.createRow(i + 3);
*
* String[] stuInfo = list.get(i);
*
* for (int j = 0; j < stuInfo.length; j++) {
*
* cell = row.createCell(j);
*
* cell.setCellStyle(tableStyle);
*
* cell.setCellValue(new HSSFRichTextString(stuInfo[j]));
*
* } }
*/
// 新建一输出文件流
FileOutputStream fOut = new FileOutputStream(path);
// 把相应的Excel 工作簿存盘
workBook.write(fOut);
fOut.flush();
// 操作结束,关闭文件
fOut.close();
logger.info("文件生成。。。。。。。。。。。。。。。。。。");
} catch (Exception e) {
logger.info("excel生成失败");
}
}
// 读取excel
public static void readtoExcel(String path) {
FileInputStream is;
try {
is = new FileInputStream(new File(path));
// 构建Workbook对象, 只读Workbook对象
HSSFWorkbook wb = new HSSFWorkbook(is);
// 获取工作表中的个数
int sheetNum = wb.getNumberOfSheets();
// 打印出工作表的名称
for (int i = 0; i < sheetNum; i++) {
logger.info(wb.getSheetName(i));
}
// 取出第一个工作表
HSSFSheet sheet = wb.getSheetAt(0);
// 取出第一个工作表中的可用的行数
int rows = sheet.getPhysicalNumberOfRows();
logger.info("取得的行数:" + rows);
// 取出第一行数据
HSSFRow row = sheet.getRow(0);
// 获得列数
int cells = row.getLastCellNum();
// System.out.println("取得第一行的列数:" + cells);
for (int j = 0; j < rows; j++) {
HSSFRow sheetrows = sheet.getRow(j);
for (int k = 0; k < sheetrows.getLastCellNum(); k++) {
HSSFCell cell = sheetrows.getCell((short) k);
if (cell != null) {
logger.info("第" + j + "行 第" + k + "列的值是"
+ getValue(cell));
} else {
logger.info("第" + j + "行 第" + k + "列的值是空的");
}
}
// 查询合并的单元格
for (int y = 0; y < sheet.getNumMergedRegions(); y++) {
logger.info("第" + (y + 1) + "个合并单元格");
Region region = sheet.getMergedRegionAt(y);
int ro = region.getRowTo() - region.getRowFrom() + 1;
int co = region.getColumnTo() - region.getColumnFrom() + 1;
logger.info("起始行:" + region.getRowFrom());
logger.info("起始列:" + region.getRowTo());
logger.info("所占行:" + ro);
logger.info("所占列:" + co);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
// 使用poi去更改excel中的数据
public static void updateExcel(String fileToBeRead) {
int coloum = 1; // 比如你要获取第1列
try {
// 构建Workbook对象, 只读Workbook对象
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(
fileToBeRead));
// 获得第一个工作表
HSSFSheet sheet = workbook.getSheetAt(0);
// 获得最后的行数
logger.info("最后的行数:" + sheet.getLastRowNum());
for (int i = 0; i <= sheet.getLastRowNum(); i++) {
// 获得每一行
HSSFRow row = sheet.getRow((short) i);
if (null == row) {
continue;
} else {
// 获得每一列
HSSFCell cell = row.getCell((short) coloum);
if (null == cell) {
continue;
} else {
String value = "";
// Cell的6种类型
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_NUMERIC: // 数值型
if (HSSFDateUtil.isCellDateFormatted(cell)) {
// 如果是date类型则 ,获取该cell的date值
value = HSSFDateUtil.getJavaDate(
cell.getNumericCellValue()).toString();
java.util.Date date1 = new Date(value);
value = format.format(date1);
} else {// 纯数字
int temp = (int) cell.getNumericCellValue();
logger.info("temp========" + temp);
cell.setCellValue(temp + 1);
// value =
// String.valueOf(cell.getNumericCellValue());
}
break;
/* 此行表示单元格的内容为string类型 */
case HSSFCell.CELL_TYPE_STRING: // 字符串型
value = cell.getStringCellValue();
logger.info("字符串====" + value);
cell.setCellValue("夕阳");
break;
case HSSFCell.CELL_TYPE_FORMULA:// 公式型
// 读公式计算值
value = String.valueOf(cell.getNumericCellValue());
if (value.equals("NaN")) {// 如果获取的数据值为非法值,则转换为获取字符串
value = cell.getStringCellValue().toString();
}
cell.getCellFormula();
break;
case HSSFCell.CELL_TYPE_BOOLEAN:// 布尔
value = " " + cell.getBooleanCellValue();
break;
/* 此行表示该单元格值为空 */
case HSSFCell.CELL_TYPE_BLANK: // 空值
value = "";
break;
case HSSFCell.CELL_TYPE_ERROR: // 故障
value = "";
break;
default:
value = cell.getStringCellValue().toString();
}
/*
* System.out.println(cell.getNumericCellValue()); int
* temp = (int) cell.getNumericCellValue();
* System.out.println("temp========" + temp);
* cell.setCellValue(temp + 1);
*/
}
}
}
// 新建一输出文件流
FileOutputStream out = null;
// 操作结束,关闭文件
try {
out = new FileOutputStream(fileToBeRead);
// 把相应的Excel 工作簿存盘
workbook.write(out);
out.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 获得表中的数据
*
* @param sheetName
* 表格索引(EXCEL 是多表文档,所以需要输入表索引号)
* @return 由LIST构成的行和表
*/
public void getDatasInSheet(String fileToBeRead) {
// 构建Workbook对象, 只读Workbook对象
HSSFWorkbook workbook;
try {
workbook = new HSSFWorkbook(new FileInputStream(fileToBeRead));
// 获得第一个工作表
HSSFSheet sheet = workbook.getSheetAt(0);
// 获得指定的表
// HSSFSheet sheet = workbook.getSheet("会员");
// 获得数据总行数
int rowCount = sheet.getLastRowNum();
if (rowCount < 1) {
return;
}
// 逐行读取数据
for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) {
// 获得行对象
HSSFRow row = sheet.getRow(rowIndex);
if (row != null) {
// 获得本行中单元格的个数
int columnCount = sheet.getRow(0).getLastCellNum();
logger.info(rowIndex + "columnCount===="
+ columnCount);
// 获得本行中各单元格中的数据
for (short columnIndex = 0; columnIndex < columnCount; columnIndex++) {
HSSFCell cell = row.getCell(columnIndex);
// 获得指定单元格中数据
Object cellStr = this.getCellString(cell);
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 获得单元格中的内容
*
* @param cell
* @return result
*/
protected Object getCellString(HSSFCell cell) {
Object result = null;
if (cell != null) {
int cellType = cell.getCellType();
switch (cellType) {
case HSSFCell.CELL_TYPE_STRING:
result = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_NUMERIC:
result = cell.getNumericCellValue();
break;
case HSSFCell.CELL_TYPE_FORMULA:
result = cell.getNumericCellValue();
break;
case HSSFCell.CELL_TYPE_ERROR:
result = null;
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
result = cell.getBooleanCellValue();
break;
case HSSFCell.CELL_TYPE_BLANK:
result = null;
break;
}
}
return result;
}
// 根据坐标去修改excel中的数据
@SuppressWarnings("deprecation")
public static void setSheetCellValue(String fileToBeRead, int rowIndex,
int colIndex, String value ) throws ParseException {
try {
// 构建Workbook对象, 只读Workbook对象
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(
fileToBeRead));
// 获得工作表的个数
int sheetNum = workbook.getNumberOfSheets();
logger.info("工作表个数 :" + sheetNum);
// 打印出工作表的名称
for (int num = 0; num < sheetNum; num++) {
logger.info("工作表名称:" + workbook.getSheetName(num));
}
// 获得第一个工作表
HSSFSheet sheet = workbook.getSheetAt(0);
// 取出工作表中的可用的总行行数
int rows = sheet.getPhysicalNumberOfRows();
logger.info("取得的总行数:" + rows);
// 循环总行数
for (int i = 0; i <= rows; i++) {
// 获得行对象
HSSFRow row = sheet.getRow((short) i);
if (null == row) {
continue;
} else {
// 获得本行中单元格的个数
int columnCount = sheet.getRow(0).getLastCellNum();
logger.info(rowIndex + "columnCount===="+ columnCount);
// 获得本行中各单元格中的数据
for (short columnIndex = 0; columnIndex < columnCount; columnIndex++) {
// 获得每一列
HSSFCell cell = row.getCell(columnIndex);
if (null == cell) {
continue;
} else {
// 行坐标
int row_Index = cell.getRowIndex();
// 列坐标
int colum_Index = cell.getColumnIndex();
logger.info("行坐标:" + row_Index + " 列坐标: "
+ colum_Index);
if (row_Index == rowIndex&& colum_Index == colIndex) {
// Cell的6种类型
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_NUMERIC: // 数值型
if (HSSFDateUtil.isCellDateFormatted(cell)) {
// 如果是date类型则 ,获取该cell的date值
//value = HSSFDateUtil.getJavaDate(cell.getNumericCellValue()).toString();
//java.util.Date date1 = new Date(value);
Date date = format.parse(value);
cell.setCellValue(date);
} else {// 纯数字
//int temp = (int) cell.getNumericCellValue();
//System.out.println("temp========"+ temp);
cell.setCellValue(value);
// value =
// String.valueOf(cell.getNumericCellValue());
}
break;
/* 此行表示单元格的内容为string类型 */
case HSSFCell.CELL_TYPE_STRING: // 字符串型
//value = cell.getStringCellValue();
logger.info("字符串====" + value);
cell.setCellValue(value);
break;
case HSSFCell.CELL_TYPE_FORMULA:// 公式型
// 读公式计算值
value = String.valueOf(cell
.getNumericCellValue());
if (value.equals("NaN")) {// 如果获取的数据值为非法值,则转换为获取字符串
value = cell.getStringCellValue().toString();
}
cell.getCellFormula();
break;
case HSSFCell.CELL_TYPE_BOOLEAN:// 布尔
value = " " + cell.getBooleanCellValue();
break;
/* 此行表示该单元格值为空 */
case HSSFCell.CELL_TYPE_BLANK: // 空值
value = "";
break;
case HSSFCell.CELL_TYPE_ERROR: // 故障
value = "";
break;
default:
value = cell.getStringCellValue()
.toString();
}
}
}
}
}
}
// 新建一输出文件流
FileOutputStream out = null;
// 操作结束,关闭文件
try {
out = new FileOutputStream(fileToBeRead);
// 把相应的Excel 工作簿存盘
workbook.write(out);
out.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
// 直接修改excel中数据
public static void setcellSheetValue(HSSFSheet sheet, int rowIndex,
int colIndex, Object value, int cellType) {
HSSFRow row = sheet.getRow(rowIndex);
HSSFCell cell = row.getCell(colIndex);
cell.setCellType(cellType);
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
cell.setCellValue(value.toString());
break;
case Cell.CELL_TYPE_NUMERIC:
cell.setCellValue(Integer.parseInt(value.toString()));
break;
default:
cell.setCellValue(value.toString());
}
}
/**
*
* @Description: 创建标准excel
* @author: GuXiYang
* @date: 2016-10-24 下午2:39:26
* @param path
*/
public static void createSheet(String path,List<Map<String, Object>> mapList) {
try {
HSSFWorkbook workBook = new HSSFWorkbook();// 创建 一个excel文档对象
HSSFSheet sheet = workBook.createSheet();// 创建一个工作薄对象
// 设置样式
HSSFCellStyle titleStyle = workBook.createCellStyle();// 创建样式对象
titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER_SELECTION);// 水平居中
titleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直居中
// 设置字体
HSSFFont titleFont = workBook.createFont();// 创建字体对象
titleFont.setFontHeightInPoints((short) 12);// 设置字体大小
titleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 设置粗体
titleFont.setFontName("黑体");// 设置为黑体字
titleStyle.setFont(titleFont);
// 合并单元格操作
sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 8));
HSSFRow row = null;
HSSFCell cell = null;
row = sheet.createRow(0); // 第一行
cell = row.createCell(0);// 第一列
cell.setCellValue(new HSSFRichTextString("中国建设银行"));
row = sheet.createRow(1); // 第二行
cell = row.createCell(0);// 第一列
cell.setCellStyle(titleStyle);// 添加样式
cell.setCellValue(new HSSFRichTextString("代发代扣流水查询下载"));
row = sheet.createRow(2); // 第三行
cell = row.createCell(0);// 第一列
cell.setCellValue(new HSSFRichTextString(""));
// 设置表文样式
HSSFCellStyle tableStyle = workBook.createCellStyle();
tableStyle.setBorderBottom((short) 1);
tableStyle.setBorderTop((short) 1);
tableStyle.setBorderLeft((short) 1);
tableStyle.setBorderRight((short) 1);
tableStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 设置表文字体
HSSFFont tableFont = workBook.createFont();
tableFont.setFontHeightInPoints((short) 10); // 设置字体大小
tableFont.setFontName("黑体"); // 设置为黑体字
tableStyle.setFont(tableFont);
//定义标题头
String[] title = {"序号","收款账号","收款人姓名","金 额 ","摘要","交易结果","失败原因","备注1","备注2" };
row = sheet.createRow(3); // 创建第四行
for (int i = 0; i < title.length; i++) {
cell = row.createCell(i);
cell.setCellStyle(tableStyle);
cell.setCellValue(new HSSFRichTextString(title[i]));
}
//随机数
int count=0;
//循环数据
for (int i = 0; i < mapList.size(); i++) {
count++;
Map<String, Object> map=mapList.get(i);
logger.info("对象中的值:"+map);
row = sheet.createRow(i + 4);
cell = row.createCell(0);
cell.setCellValue(new HSSFRichTextString(count+""));
cell = row.createCell(1);
cell.setCellValue(new HSSFRichTextString(map.get("ACCOUNT_NO").toString()));
cell = row.createCell(2);
cell.setCellValue(new HSSFRichTextString(map.get("ACCOUNT_NAME").toString()));
cell = row.createCell(3);
cell.setCellValue(new HSSFRichTextString(map.get("TRANS_AMOUNT").toString()));
cell = row.createCell(4);
cell.setCellValue(new HSSFRichTextString(count+""));
cell = row.createCell(5);
cell.setCellValue(new HSSFRichTextString("成功"));
cell = row.createCell(6);
cell.setCellValue(new HSSFRichTextString(""));
cell = row.createCell(7);
cell.setCellValue(new HSSFRichTextString(new Random().nextInt(999999999)+""));
cell = row.createCell(8);
cell.setCellValue(new HSSFRichTextString((String)map.get("SETTLE_SEQUENCE_NO")));
}
row = sheet.createRow(mapList.size() + 4);
cell = row.createCell(0);
cell.setCellValue(new HSSFRichTextString(""));
// 新建一输出文件流
FileOutputStream fOut = new FileOutputStream(path);
// 把相应的Excel 工作簿存盘
workBook.write(fOut);
fOut.flush();
fOut.close();
} catch (FileNotFoundException e) {
logger.error("文件或目录没有找到!"+path,e);
}catch (IOException e) {
logger.error("文件流异常!",e);
}catch (Exception e) {
logger.error("创建excel失败!",e);
}
}
public static void main(String[] args) {
PoiCommonUtil poi = new PoiCommonUtil();
//读取excel中的数据
// poi.readExcel("f://userinfo.xls");
// 创建excel
/** Excel 文件要存放的位置,假定在D盘JTest目录下 */
// String path = "c://xiyang.xls";
// poi.createExcel(path);
// poi.createSheet("c://李四.xls");
poi.createSheet("c://建行.xls",null);
//修改excel中的数据
//poi.updateExcel("f://userinfo.xls");
//获得表格中的对象
//poi.getDatasInSheet("f://userinfo.xls");
//根据坐标修改表中的数据
// poi.setSheetCellValue("f://userinfo.xls",2,2,"2012-05-14");
// try {
// FileOutputStream fos=new FileOutputStream("C:/13.xls");
//
// Workbook wb=new HSSFWorkbook();
//
// Sheet sheet=wb.createSheet();
// /*
// * 设定合并单元格区域范围
// * firstRow 0-based
// * lastRow 0-based
// * firstCol 0-based
// * lastCol 0-based
// */
// CellRangeAddress cra=new CellRangeAddress(0, 3, 3, 9);
//
// //在sheet里增加合并单元格
// sheet.addMergedRegion(cra);
//
// Row row = sheet.createRow(0);
//
// Cell cell_1 = row.createCell(3);
//
// cell_1.setCellValue("When you're right , no one remembers, when you're wrong ,no one forgets .");
//
// //cell 位置3-9被合并成一个单元格,不管你怎样创建第4个cell还是第5个cell…然后在写数据。都是无法写入的。
// Cell cell_2 = row.createCell(10);
//
// cell_2.setCellValue("what's up ! ");
//
// wb.write(fos);
// fos.close();
// } catch (Exception e) {
// e.printStackTrace();
// }
}
}
Poi 生成excle工具类
最新推荐文章于 2023-01-05 16:59:15 发布