当前位置:首页 » 网页前端 » javawebexcel导出
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

javawebexcel导出

发布时间: 2022-04-16 14:47:58

Ⅰ java怎么从页面导出excel

分享一个写过的例子,网上应该找的到的
response.setHeader("Content-disposition",
"attachment;filename="+ URLEncoder.encode("表名", "utf-8")+".xls");
response.setContentType("application/msexcel;charset=GBK");
ServletOutputStream writer = response.getOutputStream();
ExcelUtil excel = new ExcelUtil(ExcelUtil.ExcelType.XLS, "表名" );

String[] excelHeadRow1 = new String[] {"序号","人员编号", "姓名", "1", "2","3","4","5","6","7","8","备注"};
int uiCount;
for(uiCount=0;uiCount<userInfoList.size();uiCount++){
UserInfo ui=userInfoList.get(uiCount);
excel.addCell(0, uiCount + 3, ""+(uiCount+1), cesHeader);//序号
excel.addCell(1, uiCount + 3, ui.getPersonNo(), cesHeader);//人员编号
excel.addCell(2, uiCount + 3, ui.getName(), cesHeader);//名字
excel.addCell(3, uiCount + 3, ui.getJobLevel(), cesHeader);//1
excel.addCell(4, uiCount + 3, money, cesHeader);// 2
excel.addCell(5, uiCount + 3, "1", cesHeader);//3
excel.addCell(6, uiCount + 3, money1, cesHeader);//4
excel.addCell(7, uiCount + 3, "1", cesHeader);//5
excel.addCell(8, uiCount + 3, "", cesHeader);//6
excel.addCell(9, uiCount + 3, "", cesHeader); //7
excel.addCell(10, uiCount + 3, String.valueOf(total), cesHeader);//8
excel.addCell(11, uiCount + 3, "", cesHeader); //备注
}

Ⅱ 如何导出java工程项目里面的excel文件

request.setCharacterEncoding("utf-8");
String title = request.getParameter("title");
//title = URLDecoder.decode(title,"utf-8");
int maid = Integer.parseInt(request.getParameter("maid"));
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition" ,"attachment;filename="+new String((title+".xls").getBytes(),"ISO-8859-1"));
//ManuscriptAction down=new ManuscriptAction();

WebApplicationContext webAppContext = WebApplicationContextUtils.(getServletContext());//得到WebApplicationContext 。
ManuscriptDaoImp cocAutoService = (ManuscriptDaoImp) webAppContext.getBean("manuscriptDaoImp");
cocAutoService.exportMan(maid,title,response.getOutputStream());

out.clear();
out = pageContext.pushBody();

我的下载代码 提供你参考

Ⅲ 在Java Web开发时怎么将页面上的数据用Excel报表导出来

有两种方法一个是用POI,另一种是JXL都是别人的包,自己操作到处excel
自己在前端写一个弹出窗口选择路径之后传到代码里操作
并不能像javaswing那样方便的使用导入导出功能web需要自己实现

Ⅳ 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文件的方式

1微软提供的PAI方式 ;
优点是:API比较全,可以实现excel提供的各种需求;
缺点:和office框架绑定,服务器端还需要配置com组件,有时配置了也调不了,原因不清;
导出的速度慢;
2微软提供的VSTO:
基于excel上开发
缺点是:不好嵌入到web中 ;
3种是poi,apache提供的第三方包:
优点:速度快;
缺点:支持office版本比较有限制;
4openxml4j;
优点:速度快支持office2012,版本比较高

Ⅵ java excel导出到用户本地

一般来说做下载功能的确是先导到服务器的一个临时目录上的,然后再用一段代码把这个excel读出来,并且输出到response流里面去,给你一段可以用的代码

//------------------------------
//step1.保存一个临时excel到temp目录下
//------------------------------
//这部分自己实现,我相信你已经实现了
//假设你已经实现了保存一个excel到一个临时文件夹里面去
//并且已经生成了一个File指向这个临时的excel,名叫exportFile

//-------------------------------
//step2.弹出下载对话框
//-------------------------------
if(exportFile==null){
logger.error("生成excel错误!exportFile为空");
return;
}

//先建立一个文件读取流去读取这个临时excel文件
FileInputStreamfs=null;
try{
fs=newFileInputStream(exportFile);
}catch(FileNotFoundExceptione){
logger.error("生成excel错误!"+exportFile+"不存在!",e);
return;
}
//设置响应头和保存文件名
HttpServletResponseresponse=ServletActionContext.getResponse();
//这个一定要设定,告诉浏览器这次请求是一个下载的数据流
response.setContentType("APPLICATION/OCTET-STREAM");
try{
//这边的"采购部门本月采购报表.xls"替换成你自己要显示给用户的文件名
excelName=URLEncoder.encode("采购部门本月采购报表.xls","UTF-8");
}catch(){
logger.error("转换excel名称编码错误!",e1);
}
response.setHeader("Content-Disposition","attachment;filename=""+excelName+""");
//写出流信息
intb=0;
try{
//这里的response就是你servlet的那个传参进来的response
PrintWriterout=response.getWriter();
while((b=fs.read())!=-1){
out.write(b);
}
fs.close();
out.close();
logger.debug(sheetName+"文件下载完毕.");
}catch(Exceptione){
logger.error(sheetName+"下载文件失败!.",e);
}


把这段代码放到你的servlet的最后一部分

Ⅶ java web项目: 一个excel文件以二进制的形式存在数据库中 如何将它导出并

poi读取即可。
/**
* 读取Excel数据内容
* @param InputStream
* @return Map 包含单元格数据内容的Map对象
*/
public Map<Integer, String> readExcelContent(InputStream is) {
Map<Integer, String> content = new HashMap<Integer, String>();
String str = "";
try {
fs = new POIFSFileSystem(is);
wb = new HSSFWorkbook(fs);
} catch (IOException e) {
e.printStackTrace();
}
sheet = wb.getSheetAt(0);
// 得到总行数
int rowNum = sheet.getLastRowNum();
row = sheet.getRow(0);
int colNum = row.getPhysicalNumberOfCells();
// 正文内容应该从第二行开始,第一行为表头的标题
for (int i = 1; i <= rowNum; i++) {
row = sheet.getRow(i);
int j = 0;
while (j < colNum) {
// 每个单元格的数据内容用"-"分割开,以后需要时用String类的replace()方法还原数据
// 也可以将每个单元格的数据设置到一个javabean的属性中,此时需要新建一个javabean
// str += getStringCellValue(row.getCell((short) j)).trim() +
// "-";
str += getCellFormatValue(row.getCell((short) j)).trim() + " ";
j++;
}
content.put(i, str);
str = "";
}
return content;
}

Ⅷ 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);
}

/**
* <p>
* Description:exportToExcelFile(short rownum,short cellnum,double value)方法:
* </p>
* <p>
* 将double数据写入Execl文件的表中
* </p>
*
* @param rownum
* @param cellnum
* @param value
*/
public void exportToExcelFile(int rownum, int cellnum, double 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);
}

/**
* <p>
* Description:exportToExcelFile(short rownum,short cellnum,boolean
* value)方法:
* </p>
* <p>
* 将boolean数据写入Execl文件的表中
* </p>
*
* @param rownum
* @param cellnum
* @param value
*/
public void exportToExcelFile(int rownum, int cellnum, boolean 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);
}

/**
* <p>
* Description:exportToExcelFile(short rownum,short cellnum,Date value)方法:
* </p>
* <p>
* 将Date数据写入Execl文件的表中
* </p>
*
* @param rownum
* @param cellnum
* @param value
*/
public void exportToExcelFile(int rownum, int cellnum, Date 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);
}

/**
* <p>
* Description:exportToExcelFile(short rownum,short cellnum,Calendar
* value)方法:
* </p>
* <p>
* 将Calendar数据写入Execl文件的表中
* </p>
*
* @param rownum
* @param cellnum
* @param value
*/
public void exportToExcelFile(int rownum, int cellnum, Calendar 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);
}
/**
* 合并单元格 2012-3-30 孙贵春 add
* @param startRow
* @param startCol
* @param endRow
* @param endCol
*/
public void setMergedRegion(int startRow,int startCol,int endRow,int endCol){
sheet.addMergedRegion(new CellRangeAddress(startRow,endRow,startCol,endCol));//这与1.5版本明显不同
}
/**
* 设置单元格样式
* @param a_pos
* @param v_pos
* @param border
* @param wrap
*/
public void setCellStyle(String a_pos,String v_pos,String border,Boolean wrap){
cellStyle=wb.createCellStyle();
//水平位置
if(a_pos.equals("center")){
cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
}else if(a_pos.equals("left")){
cellStyle.setAlignment(CellStyle.ALIGN_LEFT);
}else{
cellStyle.setAlignment(CellStyle.ALIGN_RIGHT);
}
//垂直位置
if(v_pos.equals("center")){
cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
}else if(v_pos.equals("top")){
cellStyle.setVerticalAlignment(CellStyle.VERTICAL_TOP);
}else{
cellStyle.setVerticalAlignment(CellStyle.VERTICAL_BOTTOM);
}
//边框
if(border.equals("thin")){
cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
cellStyle.setBorderLeft(CellStyle.BORDER_THIN);
cellStyle.setBorderRight(CellStyle.BORDER_THIN);
cellStyle.setBorderTop(CellStyle.BORDER_THIN);
}else if(border.equals("double")){
cellStyle.setBorderBottom(CellStyle.BORDER_DOUBLE);
cellStyle.setBorderLeft(CellStyle.BORDER_DOUBLE);
cellStyle.setBorderRight(CellStyle.BORDER_DOUBLE);
cellStyle.setBorderTop(CellStyle.BORDER_DOUBLE);
}else if(border.equals("thick")){
cellStyle.setBorderBottom(CellStyle.BORDER_THICK);
cellStyle.setBorderLeft(CellStyle.BORDER_THICK);
cellStyle.setBorderRight(CellStyle.BORDER_THICK);
cellStyle.setBorderTop(CellStyle.BORDER_THICK);
}else if(border.equals("medium")){
cellStyle.setBorderBottom(CellStyle.BORDER_MEDIUM);
cellStyle.setBorderLeft(CellStyle.BORDER_MEDIUM);
cellStyle.setBorderRight(CellStyle.BORDER_MEDIUM);
cellStyle.setBorderTop(CellStyle.BORDER_MEDIUM);
}else if(border.equals("none")){
cellStyle.setBorderBottom(CellStyle.BORDER_NONE);
cellStyle.setBorderLeft(CellStyle.BORDER_NONE);
cellStyle.setBorderRight(CellStyle.BORDER_NONE);
cellStyle.setBorderTop(CellStyle.BORDER_NONE);
}else if(border.equals("dotted")){
cellStyle.setBorderBottom(CellStyle.BORDER_DOTTED);
cellStyle.setBorderLeft(CellStyle.BORDER_DOTTED);
cellStyle.setBorderRight(CellStyle.BORDER_DOTTED);
cellStyle.setBorderTop(CellStyle.BORDER_DOTTED);
}
//设置自动换行
cellStyle.setWrapText(wrap);
/*
cellStyle.setRotation((short)90);//设置单元格内文字旋转角度
XSSFFont font=wb.createFont();
font.setFamily(1);
font.setBold(true);
cellStyle.setFont(font);
*/
}

/**
* <p>
* Description: closeFileOut()方法:关闭文件输出流
* </p>
*
* @throws IOException
*/
public void closeFileOut() throws IOException {

fileOut = new FileOutputStream(outputFilename);
wb.write(fileOut);
fileOut.close();
}

/**
* <p>
* 测试方法
* </p>
*
* @param arg
*/
public static void main(String arg[]) {

try {
WriteExcelNew writeExcel = new WriteExcelNew("bak\\new.xls", "newsheet");
writeExcel.exportToExcelFile((short) 0, (short) 0, 99.99);
writeExcel.closeFileOut();
} catch (Exception e) {
System.out.println("Something Happen");
e.printStackTrace();
}
}
}

Ⅸ 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();

}

}

Ⅹ Java Web项目如何实现Excel 表格导出呢

用jxl导出吧,很方便的