主页 > 办公自动化 > poi将数据生成excel中,流程是怎么实现的?

poi将数据生成excel中,流程是怎么实现的?

2023-01-26 22:52来源:m.sf1369.com作者:宇宇

/**

* 请求处理

* @param request

* @param response

* @throws ServletException

* @throws IOException

*/

public void myself(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

//定义文件的绝对路径

String realpath=request.getSession().getServletContext().getRealPath();

System.out.println(realpath--+realpath);

//获得跳转页面的路径

String url=request.getParameter(url);

System.out.println(url--+url);

String filepath=ExcelMaker.makeExcel(excelModelPath,realpath);

System.out.println(newfilepath--+filepath);

if(url!=null&&url.length()>0){

request.setAttribute(filepath, filepath);

request.getRequestDispatcher(url).forward(request, response);

}

}

/**

* 生成excel文件

* @author 尹义恒

*

*/

public class ExcelMaker {

private static String filepath=;

private static String newfilepath=;

//通过excel模板生成

public static String makeExcel(String modelpath,String realpath){

try {

//选择模板文件

Workbook wb=Workbook.getWorkbook(new File(realpath+/yyh/+modelpath));

//创建一个新的文件

newfilepath=/yyh/yyh.xls;

filepath=realpath+newfilepath;

File targetFile=new File(filepath);

targetFile.createNewFile();

//给新的文件加载模板

WritableWorkbook wwb=Workbook.createWorkbook(targetFile, wb);

ExcelMgr.makeExcel(wwb,realpath);

wwb.write();

wwb.close();

wb.close();

} catch (Exception e) {

}

return newfilepath;

}

//不通过模板生成

public static String makeExcel(String realpath){

try {

filepath=realpath+/yyh.xls;

WritableWorkbook wwb=Workbook.createWorkbook(new File(filepath));

ExcelMgr.makeExcel(wwb,realpath);

} catch (IOException e) {

e.printStackTrace();

}

return filepath;

}

}

/**

* excel文件的管理

* @author 尹义恒

*

*/

public class ExcelMgr {

//定义一个存储数据的集合

private static List<YyhMyself> list=new ArrayList<YyhMyself>();

//定义一个业务层

private static YyhMyselfService service=new YyhMyselfService();

private List<YyhMyself> getList() {

return list;

}

public void setList(List<YyhMyself> list) {

this.list = list;

}

//生成excel文件

public static void makeExcel(WritableWorkbook wwb,String realpath){

init();

WritableSheet sheet=wwb.getSheet(0);

//加载样式

FormatStyle.init();

fillCell(sheet,realpath);

}

//加载数据

public static void init(){

//给集合加载数据

list=service.findAll();

}

//填充单元格

public static void fillCell(WritableSheet sheet,String realpath){

for(int i=0;i<list.size();i++){

YyhMyself yyh=list.get(i);

int co=yyh.getCo();

int ro=yyh.getRo();

String value=yyh.getVal();

String type=yyh.getType();

try {

if(type.equals(string)){

System.out.println(string--+value);

Label cell=new Label( co,ro,value,FormatStyle.detFormat);

sheet.addCell(cell);

}else if(type.equals(int)){

System.out.println(int--+value);

int val=new Integer(value);

jxl.write.Number cell=new jxl.write.Number(co,ro,val,FormatStyle.int_format);

sheet.addCell(cell);

}else if(type.equals(times)){

System.out.println(date--+value);

SimpleDateFormat sdf=new SimpleDateFormat(yyyy-MM-dd);

Date date=sdf.parse(value);

DateTime cell=new DateTime(co, ro, date, FormatStyle.date_format);

sheet.addCell(cell);

}else if(type.equals(img)){

System.out.println(img--+value);

System.out.println(realpath--+realpath);

File image=new File(realpath+/+value);

WritableImage img=new WritableImage(co, ro, 2, 2, image);

sheet.addImage(img);

}else if(type.equals(double)){

System.out.println(double--+value);

double val=new Double(value);

jxl.write.Number cell=new jxl.write.Number(co,ro,val,FormatStyle.double_format);

sheet.addCell(cell);

}else if(type.equals(float)){

System.out.println(float--+value);

float val=new Float(value);

jxl.write.Number cell= new jxl.write.Number(co,ro,val,FormatStyle.float_format);

sheet.addCell(cell);

}else{

System.out.println(没有查到对应的数据!);

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

}

基本流程就是这样

相关推荐

企业办公软件都有哪些?

办公自动化 2024-01-14

数控车床自动编程用什么软件?

办公自动化 2024-01-12

写字间物业管理方案

办公自动化 2023-12-09

自动化的好处和坏处

办公自动化 2023-12-05

单片机有什么特征?

办公自动化 2023-11-27

excel2007教程|excel2007教程下载

办公自动化 2023-11-22