ExcelWriter

 /**
* @author zuzhilong
* @date 2013-10-10 下午08:04:02
* @desc 生成导出Excel文件对象
* @modify
* @version 1.0.0
*/
package com.haoyisheng.util; import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Calendar; import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; public class ExcelWriter {
// 设置cell编码解决中文高位字节截断
private static short XLS_ENCODING = HSSFCell.ENCODING_UTF_16; // 定制浮点数格式
private static String NUMBER_FORMAT = "#,##0.00"; // 定制日期格式
private static String DATE_FORMAT = "m/d/yy"; // "m/d/yy h:mm" private OutputStream out = null; private HSSFWorkbook workbook = null; private HSSFSheet sheet = null; private HSSFRow row = null; public ExcelWriter() {
} /**
* 初始化Excel
*
*/
public ExcelWriter(OutputStream out) {
this.out = out;
this.workbook = new HSSFWorkbook();
this.sheet = workbook.createSheet();
} /**
* 导出Excel文件
*
* @throws IOException
*/
public void export() throws FileNotFoundException, IOException {
try {
workbook.write(out);
out.flush();
out.close();
} catch (FileNotFoundException e) {
throw new IOException(" 生成导出Excel文件出错! ", e);
} catch (IOException e) {
throw new IOException(" 写入Excel文件出错! ", e);
} } /**
* 增加一行
*
* @param index
* 行号
*/
public void createRow(int index) {
this.row = this.sheet.createRow(index);
} /**
* 获取单元格的值
*
* @param index
* 列号
*/
public String getCell(int index) {
HSSFCell cell = this.row.getCell((short) index);
String strExcelCell = "";
if (cell != null) { // add this condition
// judge
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_FORMULA:
strExcelCell = "FORMULA ";
break;
case HSSFCell.CELL_TYPE_NUMERIC: {
strExcelCell = String.valueOf(cell.getNumericCellValue());
}
break;
case HSSFCell.CELL_TYPE_STRING:
strExcelCell = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_BLANK:
strExcelCell = "";
break;
default:
strExcelCell = "";
break;
}
}
return strExcelCell;
} /**
* 设置单元格
*
* @param index
* 列号
* @param value
* 单元格填充值
*/
public void setCell(int index, int value) {
HSSFCell cell = this.row.createCell((short) index);
cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
cell.setCellValue(value);
} /**
* 设置单元格
*
* @param index
* 列号
* @param value
* 单元格填充值
*/
public void setCell(int index, double value) {
HSSFCell cell = this.row.createCell((short) index);
cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
cell.setCellValue(value);
HSSFCellStyle cellStyle = workbook.createCellStyle(); // 建立新的cell样式
HSSFDataFormat format = workbook.createDataFormat();
cellStyle.setDataFormat(format.getFormat(NUMBER_FORMAT)); // 设置cell样式为定制的浮点数格式
cell.setCellStyle(cellStyle); // 设置该cell浮点数的显示格式
} /**
* 设置单元格
*
* @param index
* 列号
* @param value
* 单元格填充值
*/
public void setCell(int index, String value) {
HSSFCell cell = this.row.createCell((short) index);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
// cell.setEncoding(XLS_ENCODING);
cell.setCellValue(value);
} /**
* 设置单元格
*
* @param index
* 列号
* @param value
* 单元格填充值
*/
public void setCell(int index, Calendar value) {
HSSFCell cell = this.row.createCell((short) index);
// cell.setEncoding(XLS_ENCODING);
cell.setCellValue(value.getTime());
HSSFCellStyle cellStyle = workbook.createCellStyle(); // 建立新的cell样式
cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat(DATE_FORMAT)); // 设置cell样式为定制的日期格式
cell.setCellStyle(cellStyle); // 设置该cell日期的显示格式
} public static void main(String[] args) {
System.out.println(" 开始导出Excel文件 "); File f = new File("d:\\qt.xls");
ExcelWriter e = new ExcelWriter(); try {
e = new ExcelWriter(new FileOutputStream(f));
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} e.createRow(0);
e.setCell(0, "试题编码 ");
e.setCell(1, "题型");
e.setCell(2, "分值");
e.setCell(3, "难度");
e.setCell(4, "级别");
e.setCell(5, "知识点"); e.createRow(1);
e.setCell(0, "t1");
e.setCell(1, 1);
e.setCell(2, 3.0);
e.setCell(3, 1);
e.setCell(4, "重要");
e.setCell(5, "专业"); try {
e.export();
System.out.println(" 导出Excel文件[成功] ");
} catch (IOException ex) {
System.out.println(" 导出Excel文件[失败] ");
ex.printStackTrace();
}
} }

ExcelReader

 /**
* @author zuzhilong
* @date 2013-10-10 下午08:02:22
* @desc 读取xls工具类
* @modify
* @version 1.0.0
*/
package com.haoyisheng.util; import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date; import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
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.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.CellStyle; public class ExcelReader {
// 工作薄,也就是一个excel文件
private HSSFWorkbook wb = null;// book [includes sheet]
//一个excle文件可以有多个sheet
private HSSFSheet sheet = null;
// 代表了表的第一行,也就是列名
private HSSFRow row = null;
// 一个excel有多个sheet,这是其中一个
private int sheetNum = 0; // 第sheetnum个工作表
// 一个sheet中可以有多行,这里应该是给行数的定义
private int rowNum = 0;
// 文件输入流
private FileInputStream fis = null;
// 指定文件
private File file = null; public ExcelReader() {
} public ExcelReader(File file) {
this.file = file;
} public void setRowNum(int rowNum) {
this.rowNum = rowNum;
} public void setSheetNum(int sheetNum) {
this.sheetNum = sheetNum;
} public void setFile(File file) {
this.file = file;
} /**
* 读取excel文件获得HSSFWorkbook对象
*/
public void open() throws IOException {
fis = new FileInputStream(file);
wb = new HSSFWorkbook(new POIFSFileSystem(fis));
fis.close();
} /**
* 返回sheet表数目
*
* @return int
*/
public int getSheetCount() {
int sheetCount = -1;
sheetCount = wb.getNumberOfSheets();
return sheetCount;
} /**
* sheetNum下的记录行数
*
* @return int
*/
public int getRowCount() {
if (wb == null)
System.out.println("=============>WorkBook为空");
HSSFSheet sheet = wb.getSheetAt(this.sheetNum);
int rowCount = -1;
rowCount = sheet.getLastRowNum();
return rowCount;
} /**
* 读取指定sheetNum的rowCount
*
* @param sheetNum
* @return int
*/
public int getRowCount(int sheetNum) {
HSSFSheet sheet = wb.getSheetAt(sheetNum);
int rowCount = -1;
rowCount = sheet.getLastRowNum();
return rowCount;
} /**
* 得到指定行的内容
*
* @param lineNum
* @return String[]
*/
public String[] readExcelLine(int lineNum) {
return readExcelLine(this.sheetNum, lineNum);
} /**
* 指定工作表和行数的内容
*
* @param sheetNum
* @param lineNum
* @return String[]
*/
public String[] readExcelLine(int sheetNum, int lineNum) {
if (sheetNum < 0 || lineNum < 0)
return null;
String[] strExcelLine = null;
try {
sheet = wb.getSheetAt(sheetNum);
row = sheet.getRow(lineNum); int cellCount = row.getLastCellNum();
strExcelLine = new String[cellCount + 1];
for (int i = 0; i <= cellCount; i++) {
strExcelLine[i] = readStringExcelCell(lineNum, i);
}
} catch (Exception e) {
e.printStackTrace();
}
return strExcelLine;
} /**
* 读取指定列的内容
*
* @param cellNum
* @return String
*/
public String readStringExcelCell(int cellNum) {
return readStringExcelCell(this.rowNum, cellNum);
} /**
* 指定行和列编号的内容
*
* @param rowNum
* @param cellNum
* @return String
*/
public String readStringExcelCell(int rowNum, int cellNum) {
return readStringExcelCell(this.sheetNum, rowNum, cellNum);
} /**
* 指定工作表、行、列下的内容
*
* @param sheetNum
* @param rowNum
* @param cellNum
* @return String
*/
public String readStringExcelCell(int sheetNum, int rowNum, int cellNum) {
if (sheetNum < 0 || rowNum < 0)
return "";
String strExcelCell = "";
try {
sheet = wb.getSheetAt(sheetNum);
row = sheet.getRow(rowNum); if (row.getCell((short) cellNum) != null) { // add this condition
// judge
switch (row.getCell((short) cellNum).getCellType()) {
case HSSFCell.CELL_TYPE_FORMULA:
strExcelCell = "FORMULA ";
break;
case HSSFCell.CELL_TYPE_NUMERIC: {
if (HSSFDateUtil.isCellDateFormatted(row.getCell((short) cellNum))) {// 处理日期格式、时间格式
SimpleDateFormat sdf = null;
if (row.getCell((short) cellNum).getCellStyle().getDataFormat() == HSSFDataFormat
.getBuiltinFormat("h:mm")) {
sdf = new SimpleDateFormat("HH:mm");
} else {// 日期
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}
Date date = row.getCell((short) cellNum).getDateCellValue();
strExcelCell = sdf.format(date);
} else if (row.getCell((short) cellNum).getCellStyle().getDataFormat() == 58) {
// 处理自定义日期格式:m月d日(通过判断单元格的格式id解决,id的值是58)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
double value = row.getCell((short) cellNum).getNumericCellValue();
Date date = org.apache.poi.ss.usermodel.DateUtil
.getJavaDate(value);
strExcelCell = sdf.format(date);
} else {
double value = row.getCell((short) cellNum).getNumericCellValue();
CellStyle style = row.getCell((short) cellNum).getCellStyle();
DecimalFormat format = new DecimalFormat("0.0");
String temp = style.getDataFormatString();
// 单元格设置成常规
if (temp.equals("General")) {
format.applyPattern("#.#");
}
strExcelCell = format.format(value);
}
}
break;
case HSSFCell.CELL_TYPE_STRING:
strExcelCell = row.getCell((short) cellNum)
.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_BLANK:
strExcelCell = "";
break;
default:
strExcelCell = "";
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return strExcelCell;
} public static void main(String args[]) {
File file = new File("d:\\无锡妇幼保健人员对应培训项目确认表(返).xls");
ExcelReader readExcel = new ExcelReader(file);
try {
readExcel.open();
} catch (IOException e) {
e.printStackTrace();
}
readExcel.setSheetNum(0); // 设置读取索引为0的工作表
// 总行数
int count = readExcel.getRowCount();
for (int i = 0; i <= count; i++) {
String[] rows = readExcel.readExcelLine(i);
for (int j = 0; j < rows.length; j++) {
System.out.print(rows[j] + " ");
}
System.out.print("\n");
}
}
}

POI-Excel表格导入和导出的更多相关文章

  1. SpringBoot中关于Excel的导入和导出

    前言   由于在最近的项目中使用Excel导入和导出较为频繁,以此篇博客作为记录,方便日后查阅.本文前台页面将使用layui,来演示对Excel文件导入和导出的效果.本文代码已上传至我的gitHub, ...

  2. excel的导入与导出---通用版

    excel的导入与导出---通用版 web项目关于导入导出的业务场景很常见,最近我就又遇到了这个业务场景.这次将最近半个月做的导入导出总结一下 使用的pom如下,主要还是阿里巴巴的easyexcel依 ...

  3. .net数据库实现Excel的导入与导出

    .net数据库实现Excel的导入与导出 参考路径:https://www.cnblogs.com/splendidme/archive/2012/01/05/2313314.html 1.defau ...

  4. 使用 EPPlus 封装的 excel 表格导入功能 (.net core c#)

    使用 EPPlus 封装的 excel 表格导入功能 前言 最近做系统的时候有很多 excel导入 的功能,以前我前后端都做的时候是在前端解析,然后再做个批量插入的接口 我觉着这样挺好的,后端部分可以 ...

  5. 使用 EPPlus 封装的 excel 表格导入功能 (二) delegate 委托 --永远滴神

    使用 EPPlus 封装的 excel 表格导入功能 (二) delegate 委托 --永远滴神 前言 接上一篇 使用 EPPlus 封装的 excel 表格导入功能 (一) 前一篇的是大概能用但是 ...

  6. 前端Excel表格导入导出,包括合并单元格,表格自定义样式等

    表格数据导入 读取导入Excel表格数据这里采用的是 xlsx 插件 npm i xlsx 读取excel需要通过 XLSX.read(data, {type: type}) 方法来实现,返回一个叫W ...

  7. SpringBoot整合easyexcel实现Excel的导入与导出

    导出 在一般不管大的或者小的系统中,各家的产品都一样,闲的无聊的时候都喜欢让我们这些程序员导出一些数据出来供他观赏,非说这是必须需求,非做不可,那么我们就只能苦逼的哼哧哼哧的写bug喽. 之前使用PO ...

  8. C#Excel的导入与导出

    1.整个Excel表格叫做工作表:WorkBook(工作薄),包含的叫页(工作表):Sheet:行:Row:单元格Cell. 2.NPOI是POI的C#版本,NPOI的行和列的index都是从0开始 ...

  9. java实现Excel的导入、导出

    一.Excel的导入 导入可采用两种方式,一种是JXL,另一种是POI,但前者不能读取高版本的Excel(07以上),后者更具兼容性.由于对两种方式都进行了尝试,就都贴出来分享(若有错误,请给予指正) ...

  10. java实现excel表格导入数据库表

    导入excel就是一个上传excel文件,然后获取excel文件数据,然后处理数据并插入到数据库的过程 一.上传excel 前端jsp页面,我的是index.jsp 在页面中我自己加入了一个下载上传文 ...

随机推荐

  1. 类的初始化__init__使用

    初始化方法: 作用: 对新创建的对象添加属性 语法: class 类名(继承列表): def __init__(self [, 形参列表]): 语句块 [] 代表中的内容可省略 说明: 1. 实始化方 ...

  2. Horizon的本地化

    1. 准备工作 apt-get install gettext horizon源码下载路径 /workspace/horizon/   2. 生成mo文件 django-admin.py makeme ...

  3. Android:Activity & Intent

    参考:<第一行代码:Android> 郭霖(著)   2.2 Activity的基本用法 隐藏标题栏 在AndroidManifest.xml中配置,作为全局配置,在所有Activity范 ...

  4. BZOJ3730 震波 【动态点分治】*

    BZOJ3730 震波 Description 在一片土地上有N个城市,通过N-1条无向边互相连接,形成一棵树的结构,相邻两个城市的距离为1,其中第i个城市的价值为value[i]. 不幸的是,这片土 ...

  5. 再也不用克隆多个仓库啦!git worktree 一个 git 仓库可以连接多个工作目录

    我在 feature 分支开发得多些,但总时不时被高优先级的 BUG 打断需要临时去 develop 分一个分支出来解 BUG.git 2.6 以上开始提供了 worktree 功能,可以解决这样的问 ...

  6. 将美化进行到底,把 PowerShell 做成 oh-my-zsh 的样子

    不知你有没有看过 Linux 上 oh-my-zsh 的样子?看过之后你一定会惊叹,原来命令行还能这么玩!然而 Windows 下能这么玩吗?答案是可行的,接下来就来看看怎么玩. Windows 下我 ...

  7. ballerina 学习二 ballerina 命令参数

    1.  目前支持的命令 run Run Ballerina program build Compile Ballerina program install Install packages to ho ...

  8. CMS搭建,织梦CMS使用教程

    http://www.dedejs.com/ 织梦DedeCms 5.7全站去版权去广告方法(含后台) http://429006.com/article/technology/3367.htm 1. ...

  9. FastAdmin 如何升级?

    FastAdmin 如何升级? 官方推荐使用 git 升级 FastAdmin. 升级 FastAdmin 核心代码 git stash git pull git stash pop 更新前端组件 比 ...

  10. RabbitMQ概念

    RabbitMQ 即一个消息队列,_主要是用来实现应用程序的异步和解耦,同时也能起到消息缓冲,消息分发的作用._RabbitMQ使用的是AMQP协议,它是一种二进制协议.默认启动端口 5672. 在 ...