好久好久没写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文件的更多相关文章

  1. java写入excel文件poi

    java写入excel文件 java写入excel文件poi,支持xlsx与xls,没有文件自动创建 package com.utils; import java.io.File; import ja ...

  2. Java读取Excel文件的几种方法

    Java读取 Excel 文件的常用开源免费方法有以下几种: 1. JDBC-ODBC Excel Driver 2. jxl.jar 3. jcom.jar 4. poi.jar 简单介绍: 百度文 ...

  3. java读取excel文件的代码

    如下内容段是关于java读取excel文件的内容,应该能对各朋友有所用途. package com.zsmj.utilit; import java.io.FileInputStream;import ...

  4. 关于解决java读取excel文件遇空行抛空指针的问题 !

    关于解决java读取excel文件遇空行抛空指针的问题 ! package exceRead; import java.io.File; import java.io.FileInputStream; ...

  5. JXL包大解析;Java程序生成excel文件和解析excel文件内容

    最近需求变化,需要把excel导入 我以前没有做过,所以我查了一些资料 和参考别人的代码 以下是多种方式: import java.io.File; import java.io.FileInputS ...

  6. Java 导入Excel文件到数据库

    原文:http://www.jb51.net/article/44021.htm 项目中要求读取excel文件内容,并将其转化为xml格式.常见读取excel文档一般使用POI和JExcelAPI这两 ...

  7. java向Excel文件写入数据

    /*使用之前要记得导入第三的jar包这个是我之前使用的时候那别人的东西自己修改了一下 还没来得及好好地封装一下还望见谅,注释我感觉写的挺清楚的就在不进行解释代码了*/package com.zzp.E ...

  8. java读写excel文件

    近期处理的数据规模比较大,正好又是统计合并的事情,想着借助excel就可以完成了,然后就了解了下java读取excel的事情. 读取的文件主要分两类:xls文件.xlsx文件.xls文件的相关操作用的 ...

  9. java 读取Excel文件并数据持久化方法Demo

    import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util ...

随机推荐

  1. Unity Svn(转)

    先吐个槽.关于这个国内各种简单到家的文章让人搞不懂,而且场景合并,prefab合并等关键问题都说没法解决,其实本质就是因为它们都是二进制文件,所以SVN没法对其合并,但事实上Unity是支持把这些文件 ...

  2. 怎样成为全栈工程师(Full Stack Developer)?

    "Facebook 工程师说 Facebook 只招 full stack engineer,那么 Facebook engineer 都是怎样的人啦."? 具体经验不重要,重要的 ...

  3. AspNet Mvc 路由解析中添加.html 等后缀 出现404错误的解决办法

    使用Mvc 有时候我们希望,浏览地址以.html .htm 等后缀名进行结尾. 于是我们就在RouteConfig 中修改路由配置信息,修改后的代码如下 routes.IgnoreRoute(&quo ...

  4. 整理常用的iOS第三方资源

    一:第三方插件 1:基于响应式编程思想的oc 地址:https://github.com/ReactiveCocoa/ReactiveCocoa 2:hud提示框 地址:https://github. ...

  5. 解析Json需要设置Mime

    IIS6.0 1.打开IIS添加Mime项 关联扩展名:*.json内容类型(MIME):application/x-javascript      2.添加映射: 位置在IIS对应站点右键属性:”主 ...

  6. cocos2dx 3.x(常见的46种动作)

    Sprite * sp= Sprite::create("Icon.png"); sp->setPosition(Vec2(, )); addChild(sp,,); // ...

  7. git如何使用 svn如何使用

    git和svn是2款常用的版本控制系统. git 的功能: 1.从服务器上克隆完整的Git仓库(包括代码和版本信息)到单机上. 也就是说自己机器上有一个git仓库. 这和svn是不同的,svn是没有本 ...

  8. First insmod a module

    不得不说网上坑爹的文章比虱子还多,参考这位仁兄调试成功 喜欢C的人却靠着Java产业吃饭,人艰不拆... 对于未知的东西,有个习惯,run success first,then research en ...

  9. ZeroClipboard 插件实现文本复制到剪贴板

    ZeroClipboard 的官网 点这里,github地址 点这里. 事例如下: 在引入 ZeroClipboard.js 之后, <button id="clip_button&q ...

  10. Android -- 仿ios上下反弹效果

    1,前几天在一个app上看到了滑动反弹效果,觉得这个效果挺不错的,然后想自己来实现一下,在网上查了一下基本上都是大致的说了下思路,自己看了一下,决定把实现的思路来详细的写下来,先看一下我们实现的效果吧 ...