Java处理excel文件
好久好久没写blog了,感觉都生锈了,最近弄了弄java处理excel,特来简单粘贴一下:
package excel; import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner; import javax.swing.JFrame; 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.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
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.ss.util.Region;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; import excel.MedicineBean; public class ExcelHandler { private String errorInfo;
private List<MedicineBean> list = new ArrayList<MedicineBean>(); /**
* 判断文件是否存在
* @param filePath
* @return
*/
public boolean validateExcel(String filePath)
{ /** 检查文件名是否为空或者是否是Excel格式的文件 */ if (filePath == null || !(WDWUtil.isExcel2003(filePath) || WDWUtil.isExcel2007(filePath)))
{ errorInfo = "文件名不是excel格式";
System.out.println(errorInfo); return false; } /** 检查文件是否存在 */ File file = new File(filePath); if (file == null || !file.exists())
{ errorInfo = "文件不存在";
System.out.println(errorInfo); return false; } return true; } /**
* 从Excel中读取数据到内存
* @param filePath
* @return
*/
public List<MedicineBean> readExcelData(String filePath){
list = new ArrayList<MedicineBean>();
FileInputStream fis=null;
if(!validateExcel(filePath))//检测文件是否合法
return null;
try {
fis=new FileInputStream(filePath);
Workbook workbook=null;
if (WDWUtil.isExcel2003(filePath))
{ POIFSFileSystem fs=new POIFSFileSystem(fis);
workbook = new HSSFWorkbook(fs);
}
else
{
workbook = new XSSFWorkbook(new BufferedInputStream(fis)); }
Sheet sheet =workbook.getSheetAt(3);
for(int i=2;i<=sheet.getLastRowNum();i++){
MedicineBean bean = new MedicineBean();
Row row=sheet.getRow(i);
Cell cell1=row.getCell(0);
Cell cell2=row.getCell(1);
Cell cell3=row.getCell(2);
Cell cell4=row.getCell(3);
Cell cell5=row.getCell(4);
Cell cell6=row.getCell(5);
Cell cell7=row.getCell(6);
Cell cell8=row.getCell(7);
Cell cell9=row.getCell(8);
Cell cell10=row.getCell(9);
Cell cell11=row.getCell(10);
SimpleDateFormat sdf = null;
sdf = new SimpleDateFormat("yyyy/MM/dd");
Date date = cell11.getDateCellValue();
bean.setId(String.valueOf(Math.round(cell1.getNumericCellValue())));
bean.setNumber(cell2.getStringCellValue());
bean.setCode(String.valueOf(Math.round(cell3.getNumericCellValue())));
bean.setRegisterName(cell4.getStringCellValue());
bean.setEnglishName(cell5.getStringCellValue());
bean.setType(cell6.getStringCellValue());
bean.setFormat(cell7.getStringCellValue());
bean.setManufacturer(cell8.getStringCellValue());
bean.setAuthorizeNumber(cell9.getStringCellValue());
bean.setRemark(cell10.getStringCellValue());
bean.setDate(sdf.format(date));
//System.out.println(bean.getId()+" "+bean.getDate()+" "+ bean.getCode()+" "+bean.getRegisterName());
list.add(bean);
}
fis.close();
return list; } catch (Exception e) {
e.printStackTrace();
return null;
}
} /**
* 数据导出到Excel表,可用于数据备份
* @param list
* @return
*/
public boolean readDataToExcelFile(List<MedicineBean> list){
try{
HSSFWorkbook workbook;
HSSFSheet sheet;
HSSFCellStyle cellstyle;
if (validateExcel("C:\\Users\\george\\Desktop\\export.xls")){
FileInputStream fs=new FileInputStream("C:\\Users\\george\\Desktop\\export.xls"); //获取d://test.xls
POIFSFileSystem ps=new POIFSFileSystem(fs); //使用POI提供的方法得到excel的信息
workbook=new HSSFWorkbook(ps);
cellstyle=workbook.createCellStyle();
sheet= workbook.getSheetAt(0); //获取到工作表,因为一个excel可能有多个工作表
} else {
HSSFRow row1;
workbook = new HSSFWorkbook();
sheet=workbook.createSheet();
workbook.setSheetName(0, "test");
row1 = sheet.createRow(0);
sheet.addMergedRegion(new Region(0,(short)0,0,(short)10));
cellstyle=workbook.createCellStyle();
HSSFCell tittleCell=row1.createCell(0);
tittleCell.setCellValue("西药药品关联信息参考数据库");
tittleCell.setCellStyle(cellstyle);
}
cellstyle.setAlignment(CellStyle.ALIGN_CENTER);
cellstyle.setVerticalAlignment(CellStyle.ALIGN_CENTER);
int lastNum = sheet.getLastRowNum();
HSSFRow dataRow=sheet.createRow(lastNum+1);//创建行
lastNum++;
HSSFCell staffcell=dataRow.createCell(0);//创建单元格
staffcell.setCellValue("ID");//设置单元格中的数据
staffcell.setCellStyle(cellstyle);//设置单元格内容居中 HSSFCell orgcell=dataRow.createCell(1);//创建单元格
orgcell.setCellValue("西药药品代码");//设置单元格中的数据
orgcell.setCellStyle(cellstyle);//设置单元格内容居中 HSSFCell syscell=dataRow.createCell(2);//创建单元格
syscell.setCellValue("药监局药品编码");//设置单元格中的数据
syscell.setCellStyle(cellstyle);//设置单元格内容居中 HSSFCell menucell=dataRow.createCell(3);//创建单元格
menucell.setCellValue("药品注册名称");//设置单元格中的数据
menucell.setCellStyle(cellstyle);//设置单元格内容居中 HSSFCell limitcell=dataRow.createCell(4);//创建单元格
limitcell.setCellValue("英文名称");//设置单元格中的数据
limitcell.setCellStyle(cellstyle);//设置单元格内容居中 HSSFCell scopecell=dataRow.createCell(5);//创建单元格
scopecell.setCellValue("药品注册剂型");//设置单元格中的数据
scopecell.setCellStyle(cellstyle);//设置单元格内容居中 HSSFCell standby1cell=dataRow.createCell(6);//创建单元格
standby1cell.setCellValue("药品注册规格");//设置单元格中的数据
standby1cell.setCellStyle(cellstyle);//设置单元格内容居中 HSSFCell standby2cell=dataRow.createCell(7);//创建单元格
standby2cell.setCellValue("生产单位");//设置单元格中的数据
standby2cell.setCellStyle(cellstyle);//设置单元格内容居中 HSSFCell standby3cell=dataRow.createCell(8);//创建单元格
standby3cell.setCellValue("批准文号");//设置单元格中的数据
standby3cell.setCellStyle(cellstyle);//设置单元格内容居中 HSSFCell standby4cell=dataRow.createCell(9);//创建单元格
standby4cell.setCellValue("批准文号备注");//设置单元格中的数据
standby4cell.setCellStyle(cellstyle);//设置单元格内容居中 HSSFCell standby5cell=dataRow.createCell(10);//创建单元格
standby5cell.setCellValue("批准日期");//设置单元格中的数据
standby5cell.setCellStyle(cellstyle);//设置单元格内容居中
for(int i=0;i<list.size();i++){
MedicineBean model=list.get(i);
dataRow=sheet.createRow(lastNum+1);//创建行
lastNum++;
staffcell=dataRow.createCell(0);//创建单元格
staffcell.setCellValue(model.getId());//设置单元格中的数据
staffcell.setCellStyle(cellstyle);//设置单元格内容居中 orgcell=dataRow.createCell(1);//创建单元格
orgcell.setCellValue(model.getNumber());//设置单元格中的数据
orgcell.setCellStyle(cellstyle);//设置单元格内容居中 syscell=dataRow.createCell(2);//创建单元格
syscell.setCellValue(model.getCode());//设置单元格中的数据
syscell.setCellStyle(cellstyle);//设置单元格内容居中 menucell=dataRow.createCell(3);//创建单元格
menucell.setCellValue(model.getRegisterName());//设置单元格中的数据
menucell.setCellStyle(cellstyle);//设置单元格内容居中 limitcell=dataRow.createCell(4);//创建单元格
limitcell.setCellValue(model.getEnglishName());//设置单元格中的数据
limitcell.setCellStyle(cellstyle);//设置单元格内容居中 scopecell=dataRow.createCell(5);//创建单元格
scopecell.setCellValue(model.getType());//设置单元格中的数据
scopecell.setCellStyle(cellstyle);//设置单元格内容居中 standby1cell=dataRow.createCell(6);//创建单元格
standby1cell.setCellValue(model.getFormat());//设置单元格中的数据
standby1cell.setCellStyle(cellstyle);//设置单元格内容居中 standby2cell=dataRow.createCell(7);//创建单元格
standby2cell.setCellValue(model.getManufacturer());//设置单元格中的数据
standby2cell.setCellStyle(cellstyle);//设置单元格内容居中 standby3cell=dataRow.createCell(8);//创建单元格
standby3cell.setCellValue(model.getAuthorizeNumber());//设置单元格中的数据
standby3cell.setCellStyle(cellstyle);//设置单元格内容居中 standby4cell=dataRow.createCell(9);//创建单元格
standby4cell.setCellValue(model.getRemark());//设置单元格中的数据
standby4cell.setCellStyle(cellstyle);//设置单元格内容居中 standby5cell=dataRow.createCell(10);//创建单元格
standby5cell.setCellValue(model.getDate());//设置单元格中的数据
standby5cell.setCellStyle(cellstyle);//设置单元格内容居中
}
File xlsFile=new File("C:\\Users\\george\\Desktop\\export.xls");
FileOutputStream fos=new FileOutputStream(xlsFile);
workbook.write(fos);
fos.close();
return true; }catch(Exception e){
e.printStackTrace();
return false;
} } /**
* 将数据修改并添加到edited。xls文件里
* @param bean
* @return
*/
public boolean editExcelById(MedicineBean bean){
try{
HSSFWorkbook workbook;
HSSFSheet sheet;
HSSFCellStyle cellstyle;
if (validateExcel("C:\\Users\\george\\Desktop\\edited.xls")){
FileInputStream fs=new FileInputStream("C:\\Users\\george\\Desktop\\edited.xls");
POIFSFileSystem ps=new POIFSFileSystem(fs); //使用POI提供的方法得到excel的信息
workbook=new HSSFWorkbook(ps);
cellstyle=workbook.createCellStyle();
sheet= workbook.getSheetAt(0); //获取到工作表,因为一个excel可能有多个工作表
} else {
HSSFRow row1;
workbook = new HSSFWorkbook();
sheet=workbook.createSheet();
workbook.setSheetName(0, "test");
row1 = sheet.createRow(0);
sheet.addMergedRegion(new Region(0,(short)0,0,(short)10));
cellstyle=workbook.createCellStyle();
HSSFCell tittleCell=row1.createCell(0);
tittleCell.setCellValue("西药药品关联信息参考数据库");
tittleCell.setCellStyle(cellstyle);
}
cellstyle.setAlignment(CellStyle.ALIGN_CENTER);
cellstyle.setVerticalAlignment(CellStyle.ALIGN_CENTER);
int lastNum = sheet.getLastRowNum();
HSSFRow dataRow;
HSSFCell staffcell,orgcell,syscell,menucell,limitcell,scopecell,standby1cell;
HSSFCell standby2cell,standby3cell,standby4cell,standby5cell;
if (!validateExcel("C:\\Users\\george\\Desktop\\edited.xls")){
dataRow=sheet.createRow(lastNum+1);//创建行
lastNum++;
staffcell=dataRow.createCell(0);//创建单元格
staffcell.setCellValue("ID");//设置单元格中的数据
staffcell.setCellStyle(cellstyle);//设置单元格内容居中 orgcell=dataRow.createCell(1);//创建单元格
orgcell.setCellValue("西药药品代码");//设置单元格中的数据
orgcell.setCellStyle(cellstyle);//设置单元格内容居中 syscell=dataRow.createCell(2);//创建单元格
syscell.setCellValue("药监局药品编码");//设置单元格中的数据
syscell.setCellStyle(cellstyle);//设置单元格内容居中 menucell=dataRow.createCell(3);//创建单元格
menucell.setCellValue("药品注册名称");//设置单元格中的数据
menucell.setCellStyle(cellstyle);//设置单元格内容居中 limitcell=dataRow.createCell(4);//创建单元格
limitcell.setCellValue("英文名称");//设置单元格中的数据
limitcell.setCellStyle(cellstyle);//设置单元格内容居中 scopecell=dataRow.createCell(5);//创建单元格
scopecell.setCellValue("药品注册剂型");//设置单元格中的数据
scopecell.setCellStyle(cellstyle);//设置单元格内容居中 standby1cell=dataRow.createCell(6);//创建单元格
standby1cell.setCellValue("药品注册规格");//设置单元格中的数据
standby1cell.setCellStyle(cellstyle);//设置单元格内容居中 standby2cell=dataRow.createCell(7);//创建单元格
standby2cell.setCellValue("生产单位");//设置单元格中的数据
standby2cell.setCellStyle(cellstyle);//设置单元格内容居中 standby3cell=dataRow.createCell(8);//创建单元格
standby3cell.setCellValue("批准文号");//设置单元格中的数据
standby3cell.setCellStyle(cellstyle);//设置单元格内容居中 standby4cell=dataRow.createCell(9);//创建单元格
standby4cell.setCellValue("批准文号备注");//设置单元格中的数据
standby4cell.setCellStyle(cellstyle);//设置单元格内容居中 standby5cell=dataRow.createCell(10);//创建单元格
standby5cell.setCellValue("批准日期");//设置单元格中的数据
standby5cell.setCellStyle(cellstyle);//设置单元格内容居中
}
MedicineBean model=bean;
dataRow=sheet.createRow(lastNum+1);//创建行
lastNum++;
staffcell=dataRow.createCell(0);//创建单元格
staffcell.setCellValue(model.getId());//设置单元格中的数据
staffcell.setCellStyle(cellstyle);//设置单元格内容居中 orgcell=dataRow.createCell(1);//创建单元格
orgcell.setCellValue(model.getNumber());//设置单元格中的数据
orgcell.setCellStyle(cellstyle);//设置单元格内容居中 syscell=dataRow.createCell(2);//创建单元格
syscell.setCellValue(model.getCode());//设置单元格中的数据
syscell.setCellStyle(cellstyle);//设置单元格内容居中 menucell=dataRow.createCell(3);//创建单元格
menucell.setCellValue(model.getRegisterName());//设置单元格中的数据
menucell.setCellStyle(cellstyle);//设置单元格内容居中 limitcell=dataRow.createCell(4);//创建单元格
limitcell.setCellValue(model.getEnglishName());//设置单元格中的数据
limitcell.setCellStyle(cellstyle);//设置单元格内容居中 scopecell=dataRow.createCell(5);//创建单元格
scopecell.setCellValue(model.getType());//设置单元格中的数据
scopecell.setCellStyle(cellstyle);//设置单元格内容居中 standby1cell=dataRow.createCell(6);//创建单元格
standby1cell.setCellValue(model.getFormat());//设置单元格中的数据
standby1cell.setCellStyle(cellstyle);//设置单元格内容居中 standby2cell=dataRow.createCell(7);//创建单元格
standby2cell.setCellValue(model.getManufacturer());//设置单元格中的数据
standby2cell.setCellStyle(cellstyle);//设置单元格内容居中 standby3cell=dataRow.createCell(8);//创建单元格
standby3cell.setCellValue(model.getAuthorizeNumber());//设置单元格中的数据
standby3cell.setCellStyle(cellstyle);//设置单元格内容居中 standby4cell=dataRow.createCell(9);//创建单元格
standby4cell.setCellValue(model.getRemark());//设置单元格中的数据
standby4cell.setCellStyle(cellstyle);//设置单元格内容居中 standby5cell=dataRow.createCell(10);//创建单元格
standby5cell.setCellValue(model.getDate());//设置单元格中的数据
standby5cell.setCellStyle(cellstyle);//设置单元格内容居中
File xlsFile=new File("C:\\Users\\george\\Desktop\\edited.xls");
FileOutputStream fos=new FileOutputStream(xlsFile);
workbook.write(fos);
fos.close();
return true; }catch(Exception e){
e.printStackTrace();
return false;
}
} /**
* 从键盘输入信息修改,每次一个
*/
public void inputMedicine(){
Scanner input =new Scanner(System.in);
System.out.println("输入需要更改的药品Id:");
int id = input.nextInt();
MedicineBean show = list.get(id-1);
System.out.println(show.getId() + " " + show.getRegisterName() + " " + show.getEnglishName() + " " + show.getDate());
System.out.println("输入需要更改药品信息:");
String fl = input.nextLine();
System.out.println("输入西药药品代码:");
String number = input.nextLine();
System.out.println("输入药监局药品编码:");
String code = input.nextLine();
System.out.println("输入西药药品注册名称:");
String register = input.nextLine();
System.out.println("输入药品英文名称:");
String eng = input.nextLine();
System.out.println("输入药品注册剂型:");
String type = input.nextLine();
System.out.println("输入药品注册规格:");
String format = input.nextLine();
System.out.println("输入药品生产单位:");
String man = input.nextLine();
System.out.println("输入批准文号:");
String auth = input.nextLine();
System.out.println("输入批准文号备注:");
String remark = input.nextLine();
System.out.println("输入批准日期:");
String date = input.nextLine();
MedicineBean edit = new MedicineBean();
edit.setId(show.getId());
edit.setNumber(number);
edit.setCode(code);
edit.setRegisterName(register);
edit.setEnglishName(eng);
edit.setType(type);
edit.setFormat(format);
edit.setManufacturer(man);
edit.setAuthorizeNumber(auth);
edit.setRemark(remark);
edit.setDate(date);
input.close();
editExcelById(edit);
System.out.println("更改单独内容已经输出到:edited。xls文件,请查看!"); } /**
* 主函数用于测试
* @param args
*/
public static void main(String[] args){
String filePath = "C:\\Users\\george\\Desktop\\test1.xlsx";
ExcelHandler handler = new ExcelHandler();
List<MedicineBean> list = new ArrayList<MedicineBean>();
list = handler.readExcelData(filePath);
handler.readDataToExcelFile(list);
handler.inputMedicine();
} } class WDWUtil
{ /**
*
* @描述:是否是2003的excel,返回true是2003
*
* @参数:@param filePath 文件完整路径
*
* @参数:@return
*
* @返回值:boolean
*/ public static boolean isExcel2003(String filePath)
{ return filePath.matches("^.+\\.(?i)(xls)$"); } /**
*
* @描述:是否是2007的excel,返回true是2007
*
* @参数:@param filePath 文件完整路径
*
* @参数:@return
*
* @返回值:boolean
*/ public static boolean isExcel2007(String filePath)
{ return filePath.matches("^.+\\.(?i)(xlsx)$"); } }
Java处理excel文件的更多相关文章
- java写入excel文件poi
java写入excel文件 java写入excel文件poi,支持xlsx与xls,没有文件自动创建 package com.utils; import java.io.File; import ja ...
- Java读取Excel文件的几种方法
Java读取 Excel 文件的常用开源免费方法有以下几种: 1. JDBC-ODBC Excel Driver 2. jxl.jar 3. jcom.jar 4. poi.jar 简单介绍: 百度文 ...
- java读取excel文件的代码
如下内容段是关于java读取excel文件的内容,应该能对各朋友有所用途. package com.zsmj.utilit; import java.io.FileInputStream;import ...
- 关于解决java读取excel文件遇空行抛空指针的问题 !
关于解决java读取excel文件遇空行抛空指针的问题 ! package exceRead; import java.io.File; import java.io.FileInputStream; ...
- JXL包大解析;Java程序生成excel文件和解析excel文件内容
最近需求变化,需要把excel导入 我以前没有做过,所以我查了一些资料 和参考别人的代码 以下是多种方式: import java.io.File; import java.io.FileInputS ...
- Java 导入Excel文件到数据库
原文:http://www.jb51.net/article/44021.htm 项目中要求读取excel文件内容,并将其转化为xml格式.常见读取excel文档一般使用POI和JExcelAPI这两 ...
- java向Excel文件写入数据
/*使用之前要记得导入第三的jar包这个是我之前使用的时候那别人的东西自己修改了一下 还没来得及好好地封装一下还望见谅,注释我感觉写的挺清楚的就在不进行解释代码了*/package com.zzp.E ...
- java读写excel文件
近期处理的数据规模比较大,正好又是统计合并的事情,想着借助excel就可以完成了,然后就了解了下java读取excel的事情. 读取的文件主要分两类:xls文件.xlsx文件.xls文件的相关操作用的 ...
- java 读取Excel文件并数据持久化方法Demo
import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util ...
随机推荐
- 《UML大战需求分析》阅读笔记01
在刚学习软件开发的课程时,首先学习了UML设计,但只是学习了基本的语法,虽然在学期通过课堂练习进行了实践,但并没有真正理解其中作用.为了进一步的理解UML的用法,我阅读了<UML大战需求分析&g ...
- Kafka - protocol
http://kafka.apache.org/protocol 具体的协议看原文, Preliminaries Network Kafka uses a binary protocol ov ...
- XAMPP PHP redis 扩展
1.php增加redis扩展 echo phpinfo();exit;查看php 版本以及 vc运行库 可知 X86 MSVC11 PHP5.6 首先把对应版本的php_redis.dll 和 ...
- JavaMail邮件开发
一.只带有纯文本的邮件 代码事例如下: package com.lyh.sendemail; import java.util.Properties; import javax.mail.Messag ...
- 学习mysql
一 概述 1.什么是数据库 数据库就是数据的仓库. mysql是对数据库进行存储和指令操作的软件.这类软件成为数据管理系统Database Management System. 2.mysql的安装和 ...
- oracle 判断字符串是否日期格式
select case when to_char(TO_DATE(NVL('2015- 8', 'a'), 'yyyy-mm'),'yyyy-mm')='2015- 8' then 1 else 0 ...
- Maven-010-maven 编译报错:Failure to ... in ... was cached in the local repository, resolution will not be reattempted until the update interval of nexus has elapsed or updates are forced.
今晚在编译 maven 项目的时候,命令行报错,出现 Failure to ... in ... 类似错误,详细的错误信息如下所示: [INFO] -------------------------- ...
- PL/SQL不支持64位Oracle Client 解决办法
解决X64操作系统PL/SQL连接报错问题 make sure you have the 32 bits oracle client installed 说明PLSQL Developer并不支持Or ...
- Knockout.js是什么?
从本节开始介绍关于KnockoutJs相关的内容,本节主要介绍knockoutjs一些重要特性与优点,以及它与Jquery等框架库之间的区别. 1.Knockout.js是什么? Knockout是一 ...
- VS2010的Razor智能感知和语法高亮突然消失
猜想可能是安装了VS2008的原因,尝试重新安装下面的组件,看看是否解决问题: 用于 Visual Studio 2010 SP1 和 Visual Web Developer 2010 SP1 的 ...