好久好久没写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文件-LMLPHP

05-11 11:36