本文目录一览:
java web poi如何按查询结果导出相应的Excel
package com.aerolink.aocs.util.fileUtil;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
//import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class WriteExcelNew {
/**
* Excel文件
*/
private XSSFWorkbook wb = null;
/**
* 输出Excel文件中的表对象
*/
private XSSFSheet sheet = null;
/**
* 输出文件流
*/
private FileOutputStream fileOut = null;
/**
* 输出文件名用户自定义
*/
private String outputFilename = null;
/**
* 单元格样式
*/
private XSSFCellStyle cellStyle = null;
// private String newsheet = null; //输出Excel文件中的表名用户自定义
/**
* 行
*/
private XSSFRow row=null;
/**
*
*/
private int rowNumber=-1;
/**
* @param outputFilename
* @param newsheet
*/
public WriteExcelNew(String outputFilename, String newsheet) {
wb = new XSSFWorkbook();
//wb.setSheetName(1, "qwe");//设置第一张表的名称
sheet = wb.createSheet(newsheet);
//sheet.setColumnWidth(1, 40);//第一行 列宽
this.outputFilename = outputFilename;
// this.newsheet = newsheet;
}
/**
* p
* Description:exportToExcelFile(short rownum,short cellnum,int value)方法:
* /p
* p
* 将int数据写入Execl文件的表中
* /p
*
* @param rownum
* @param cellnum
* @param value
*/
public void exportToExcelFile(int rownum, int cellnum, int value) {
if(rowNumber==-1){ //在poi3.8版本中,不这样做,只能写入最后一个单元格
rowNumber=rownum;
row = sheet.createRow(rownum);
}else if(rowNumber!=rownum){
rowNumber=rownum;
row = sheet.createRow(rownum);
}
//row.setHeight((short)50);//行高
XSSFCell cell = row.createCell(cellnum);
cell.setCellValue(value);
if(cellStyle==null){
setCellStyle("center","center","",false);
}
cell.setCellStyle(cellStyle);
}
/**
* p
* Description:exportToExcelFile(short rownum,short cellnum,String value)方法:
* /p
* p
* 将String数据写入Execl文件的表中
* /p
*
* @param rownum
* @param cellnum
* @param value
*/
public void exportToExcelFile(int rownum, int cellnum, String value) {
if(rowNumber==-1){ //在poi3.8版本中,不这样做,只能写入最后一个单元格
rowNumber=rownum;
row = sheet.createRow(rownum);
}else if(rowNumber!=rownum){
rowNumber=rownum;
row = sheet.createRow(rownum);
}
XSSFCell cell = row.createCell(cellnum);
cell.setCellValue(value);
if(cellStyle==null){
setCellStyle("center","center","",false);
}
cell.setCellStyle(cellStyle);
}
java web导出excel 数据源问题
第一,页面点击导出按钮,调用java后台查询数据。
第二,将查询出数据,进行Excel文件的写入。
第三,成功,页面提示导出成功。 失败,提示导出失败。
就这么简单,楼主不要想的太复杂了。
javaweb 导出excel需要哪些jar包?
java导出Excel需要用到poi的jar包,
// 第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet("学生表一");
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFRow row = sheet.createRow((int) 0);
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
HSSFCell cell = row.createCell((short) 0);
cell.setCellValue("学号");
cell.setCellStyle(style);
cell = row.createCell((short) 1);
cell.setCellValue("姓名");
cell.setCellStyle(style);
cell = row.createCell((short) 2);
cell.setCellValue("年龄");
cell.setCellStyle(style);
cell = row.createCell((short) 3);
cell.setCellValue("生日");
cell.setCellStyle(style);
// 第五步,写入实体数据 实际应用中这些数据从数据库得到,
List list = CreateSimpleExcelToDisk.getStudent();
for (int i = 0; i list.size(); i++)
{
row = sheet.createRow((int) i + 1);
Student stu = (Student) list.get(i);
// 第四步,创建单元格,并设置值
row.createCell((short) 0).setCellValue((double) stu.getId());
row.createCell((short) 1).setCellValue(stu.getName());
row.createCell((short) 2).setCellValue((double) stu.getAge());
cell = row.createCell((short) 3);
cell.setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(stu
.getBirth()));
}
// 第六步,将文件存到指定位置
try
{
FileOutputStream fout = new FileOutputStream("E:/students.xls");
wb.write(fout);
fout.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
微软的OFFICE是最为流行的办公软件,主要有OFFICE2010和OFFICE2007两个版本。Office 2000是第三代办公处理软件的代表产品,可以作为办公和管理的平台,以提高使用者的工作效率和决策能力。Office 2000中文版有4种不同的版本:标准版、中小企业版、中文专业版和企业版。
在Office 2000中各个组件仍有着比较明确的分工:一般说来,Word主要用来进行文本的输入、编辑、排版、打印等工作;Excel主要用来进行有繁重计算任务的预算、财务、数据汇总等工作;PowerPoint主要用来制作演示文稿和幻灯片及投影片等;Access是一个桌面数据库系统及数据库应用程序;Outlook是一个桌面信息管理的应用程序;FrontPage主要用来制作和发布因特网的Web页面。
Microsoft Office XP是微软有史以来所发行的Office版本中最重要的版本,而且也被认为是迄今为止功能最强大、最易于使用的Office产品。新版Office放弃了以往以产品发布年命名的惯例!产品名称中的XP,是英文Experience(体验)的缩写,代表着新版Office在包容覆盖广泛设备的Web服务之后,将给用户带来丰富的、充分扩展的全新体验。
除核心的 Office XP 程序 — Microsoft Word、Excel、Outlook和 PowerPoint— 外,Office XP 专业版 中包含 Microsoft Access 2002,它是 Office XP 数据库解决方案,可帮助用户存储、访问和分析数据。