导航:首页 > 编程语言 > javapoi导出xls

javapoi导出xls

发布时间:2021-03-03 23:48:56

java使用poi导出excel

找不到文件流,原因是因为,这里的inputName是你本机的路径,你本机有这个文回件,所以能下载答,但是发布到服务器上,这里的inputName就是服务器上的路径,而服务器上这个路径下没有这个文件,所以找不到文件流!

② java中poi怎么生成excel

网上好多啊 我给你找了一个例子 view plain to clipboardprint?
public static void main(String[] args) {
try {
String filepath = "d:\\问题清单.xls";
FileInputStream fis = new FileInputStream(filepath);
// POIFSFileSystem pfs = new POIFSFileSystem(new FileInputStream("d:\\OA问题清单.xls"));
// HSSFWorkbook hwb = new HSSFWorkbook(pfs);
HSSFWorkbook hwb = new HSSFWorkbook(fis);
// HSSFSheet hws = hwb.getSheetAt(0);
HSSFSheet hws = hwb.getSheet("问题清单");
HSSFRow row = hws.getRow(2);
HSSFCell cell = null;
cell = row.getCell((short) 1);
System.out.println("cellnumber:"+cell.getCellNum());
System.out.println("cellvalue:"+getExcelCellValue(cell));
cell.setCellValue(new Date());
cell = row.getCell((short)2);
HSSFRichTextString rts = new HSSFRichTextString("输入");
cell.setCellValue(rts);
FileOutputStream fos = new FileOutputStream(filepath);
hwb.write(fos);
fis.close();
fos.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(poi.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(poi.class.getName()).log(Level.SEVERE, null, ex);
}

}

public static String getExcelCellValue(HSSFCell cell) {
String ret = "";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
DecimalFormat df = new DecimalFormat("#");
int celltype = cell.getCellType();
switch(celltype){
case HSSFCell.CELL_TYPE_NUMERIC:
if(HSSFDateUtil.isCellDateFormatted(cell)){
ret = sdf.format(cell.getDateCellValue());
}else{
ret = df.format(cell.getNumericCellValue());
}
break;
case HSSFCell.CELL_TYPE_STRING:
ret = cell.getRichStringCellValue().toString();
break;
default:

}
return ret;
}

③ JAVA,POI导出EXCEL表,表中所有数据都是从后台直接获取,求指导,越详细越好

前几天刚好因抄为工作需要做过相应的excel处理程序编写,这里有一个例子,注释也很详细,具体参见doexeclsrcutilExeclUtil.java中的writeExcel方法,其中的写出文件的保存路径采用的是配置文件的形式,你也可以直接写死,具体的内容你就看代码

④ javaPOI导出Excel表格

数据库字段有空值或null不执行是你想要的效果还是说现在存在这样的问题?

⑤ java如何导出excel表格,如果用poi,java代码如何实现.,求代码!!!

项目结构:

xls:

\\\

XlsMain .java 类
//该类有main方法,主要负责运行程序,同时该类中也包含了用poi读取Excel(2003版)

*

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

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

/**
*
* @author Hongten</br>
*
* 参考地址:http://hao0610.iteye.com/blog/1160678
*
*/
public class XlsMain {

public static void main(String[] args) throws IOException {
XlsMain xlsMain = new XlsMain();
XlsDto xls = null;
List<XlsDto> list = xlsMain.readXls();

try {
XlsDto2Excel.xlsDto2Excel(list);
} catch (Exception e) {
e.printStackTrace();
}
for (int i = 0; i < list.size(); i++) {
xls = (XlsDto) list.get(i);
System.out.println(xls.getXh() + " " + xls.getXm() + " "
+ xls.getYxsmc() + " " + xls.getKcm() + " "
+ xls.getCj());
}

}

/**
* 读取xls文件内容
*
* @return List<XlsDto>对象
* @throws IOException
* 输入/输出(i/o)异常
*/
private List<XlsDto> readXls() throws IOException {
InputStream is = new FileInputStream("pldrxkxxmb.xls");
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
XlsDto xlsDto = null;
List<XlsDto> list = new ArrayList<XlsDto>();
// 循环工作表Sheet
for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {
HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
if (hssfSheet == null) {
continue;
}
// 循环行Row
for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
HSSFRow hssfRow = hssfSheet.getRow(rowNum);
if (hssfRow == null) {
continue;
}
xlsDto = new XlsDto();
// 循环列Cell
// 0学号 1姓名 2学院 3课程名 4 成绩
// for (int cellNum = 0; cellNum <=4; cellNum++) {
HSSFCell xh = hssfRow.getCell(0);
if (xh == null) {
continue;
}
xlsDto.setXh(getValue(xh));
HSSFCell xm = hssfRow.getCell(1);
if (xm == null) {
continue;
}
xlsDto.setXm(getValue(xm));
HSSFCell yxsmc = hssfRow.getCell(2);
if (yxsmc == null) {
continue;
}
xlsDto.setYxsmc(getValue(yxsmc));
HSSFCell kcm = hssfRow.getCell(3);
if (kcm == null) {
continue;
}
xlsDto.setKcm(getValue(kcm));
HSSFCell cj = hssfRow.getCell(4);
if (cj == null) {
continue;
}
xlsDto.setCj(Float.parseFloat(getValue(cj)));
list.add(xlsDto);
}
}
return list;
}

/**
* 得到Excel表中的值
*
* @param hssfCell
* Excel中的每一个格子
* @return Excel中每一个格子中的值
*/
@SuppressWarnings("static-access")
private String getValue(HSSFCell hssfCell) {
if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) {
// 返回布尔类型的值
return String.valueOf(hssfCell.getBooleanCellValue());
} else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) {
// 返回数值类型的值
return String.valueOf(hssfCell.getNumericCellValue());
} else {
// 返回字符串类型的值
return String.valueOf(hssfCell.getStringCellValue());
}
}

}

XlsDto2Excel.java类
//该类主要负责向Excel(2003版)中插入数据

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class XlsDto2Excel {

/**
*
* @param xls
* XlsDto实体类的一个对象
* @throws Exception
* 在导入Excel的过程中抛出异常
*/
public static void xlsDto2Excel(List<XlsDto> xls) throws Exception {
// 获取总列数
int CountColumnNum = xls.size();
// 创建Excel文档
HSSFWorkbook hwb = new HSSFWorkbook();
XlsDto xlsDto = null;
// sheet 对应一个工作页
HSSFSheet sheet = hwb.createSheet("pldrxkxxmb");
HSSFRow firstrow = sheet.createRow(0); // 下标为0的行开始
HSSFCell[] firstcell = new HSSFCell[CountColumnNum];
String[] names = new String[CountColumnNum];
names[0] = "学号";
names[1] = "姓名";
names[2] = "学院";
names[3] = "课程名";
names[4] = "成绩";
for (int j = 0; j < CountColumnNum; j++) {
firstcell[j] = firstrow.createCell(j);
firstcell[j].setCellValue(new HSSFRichTextString(names[j]));
}
for (int i = 0; i < xls.size(); i++) {
// 创建一行
HSSFRow row = sheet.createRow(i + 1);
// 得到要插入的每一条记录
xlsDto = xls.get(i);
for (int colu = 0; colu <= 4; colu++) {
// 在一行内循环
HSSFCell xh = row.createCell(0);
xh.setCellValue(xlsDto.getXh());
HSSFCell xm = row.createCell(1);
xm.setCellValue(xlsDto.getXm());
HSSFCell yxsmc = row.createCell(2);
yxsmc.setCellValue(xlsDto.getYxsmc());
HSSFCell kcm = row.createCell(3);
kcm.setCellValue(xlsDto.getKcm());
HSSFCell cj = row.createCell(4);
cj.setCellValue(xlsDto.getCj());
(xlsDto.getMessage());
}
}
// 创建文件输出流,准备输出电子表格
OutputStream out = new FileOutputStream("POI2Excel/pldrxkxxmb.xls");
hwb.write(out);
out.close();
System.out.println("数据库导出成功");
}

}

XlsDto .java类
//该类是一个实体类

public class XlsDto {
/**
* 选课号
*/
private Integer xkh;
/**
* 学号
*/
private String xh;
/**
* 姓名
*/
private String xm;
/**
* 学院
*/
private String yxsmc;
/**
* 课程号
*/
private Integer kch;
/**
* 课程名
*/
private String kcm;
/**
* 成绩
*/
private float cj;
public Integer getXkh() {
return xkh;
}
public void setXkh(Integer xkh) {
this.xkh = xkh;
}
public String getXh() {
return xh;
}
public void setXh(String xh) {
this.xh = xh;
}
public String getXm() {
return xm;
}
public void setXm(String xm) {
this.xm = xm;
}
public String getYxsmc() {
return yxsmc;
}
public void setYxsmc(String yxsmc) {
this.yxsmc = yxsmc;
}
public Integer getKch() {
return kch;
}
public void setKch(Integer kch) {
this.kch = kch;
}
public String getKcm() {
return kcm;
}
public void setKcm(String kcm) {
this.kcm = kcm;
}
public float getCj() {
return cj;
}
public void setCj(float cj) {
this.cj = cj;
}

}

后台输出:
数据库导出成功
1.0 hongten 信息技术学院 计算机网络应用基础 80.0
2.0 王五 信息技术学院 计算机网络应用基础 81.0
3.0 李胜基 信息技术学院 计算机网络应用基础 82.0
4.0 五班古 信息技术学院 计算机网络应用基础 83.0
5.0 蔡诗芸 信息技术学院 计算机网络应用基础 84.0

⑥ 关于java poi 导出Excel如何导出的问题。

只需要将数据添加到excel中?
---------------------
能详细举例吗?这样不是很明白。

⑦ java从数据库中导出excel poi

这个我做奖金,考勤系统的时候经常用到,是一个方法,希望能帮到你。
用的 poi:
/**
* 将奖金列表转换为奖金报表
* @param bonus
* @return byte[]
*/
private byte[] mainProcessBonusListToReport(List<Bonus> bonuses){
HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
HSSFSheet hssfSheet =hssfWorkbook.createSheet("总奖金报表单");
/*第一行单元格合并*/

hssfSheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 12));
/*第一行*/

HSSFRow hssfRow = hssfSheet.createRow(0);
HSSFCell hssfCell=hssfRow.createCell(0);
hssfCell.setCellValue("派单业务考核记录");
/*第二行*/

hssfRow = hssfSheet.createRow(1);
/*列名*/

String[] titles = {"序号","工号","话务员姓名","规范奖考核","业务奖金","话务奖金","服务质量奖金","星级系数","组长津贴","总奖金","增资奖金","英语翻译","实发总奖金"};
/*for循环生成列名*/

for (int i = 0; i < titles.length; i++) {
hssfCell = hssfRow.createCell(i);
hssfCell.setCellValue(titles[i]);
}
/*填充数据*/

int rowIndex=2;
for (Bonus bonus : bonuses) {
hssfRow = hssfSheet.createRow(rowIndex);
hssfCell = hssfRow.createCell(0);
hssfCell.setCellValue(rowIndex-1);
hssfCell = hssfRow.createCell(1);
hssfCell.setCellValue(bonus.getWorkNumber());
hssfCell = hssfRow.createCell(2);
hssfCell.setCellValue(bonus.getName());
hssfCell = hssfRow.createCell(3);
hssfCell.setCellValue(bonus.getStandardPerformance());
hssfCell = hssfRow.createCell(4);
hssfCell.setCellValue(bonus.getBusinessBonus());
hssfCell = hssfRow.createCell(5);
hssfCell.setCellValue(bonus.getCallBonus());
hssfCell = hssfRow.createCell(6);
hssfCell.setCellValue(bonus.getServiceQualityBonus());
hssfCell = hssfRow.createCell(7);
hssfCell.setCellValue(bonus.getStarCoefficient());
hssfCell = hssfRow.createCell(8);
hssfCell.setCellValue(bonus.getGroupLeaderAllowance());
hssfCell = hssfRow.createCell(9);
hssfCell.setCellValue(bonus.getTotalBonus());
hssfCell = hssfRow.createCell(10);
hssfCell.setCellValue(bonus.getAdditionalBonus());
hssfCell = hssfRow.createCell(11);
hssfCell.setCellValue(bonus.getEnglishTranslateBonus());
hssfCell = hssfRow.createCell(12);
hssfCell.setCellValue(bonus.getActualTotalBonus());
rowIndex++;
}
byte[] bytes = TypeUtils.HSSFWorkbookToByteArray(hssfWorkbook);
return bytes;
}

⑧ JAVA 使用POI导出EXCEL文件,但是数据中有特殊字符该如何正确导出

在保护状态下execl的格式有可能正在被使用,你这边修改,准确说是线程冲突,一般excel值会作为导出文件的模板,是不会编辑的。你可以在读的时候判断execl是否正在被使用。
下面的代码问题,你可以参考
package com.hwt.glmf.common;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
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.hssf.util.CellRangeAddress;
import org.apache.poi.hssf.util.HSSFColor;

/**
* 导出Excel公共方法
* @version 1.0
*
* @author wangcp
*
*/
public class ExportExcel extends BaseAction {

//显示的导出表的标题
private String title;
//导出表的列名
private String[] rowName ;

private List<Object[]> dataList = new ArrayList<Object[]>();

HttpServletResponse response;

//构造方法,传入要导出的数据
public ExportExcel(String title,String[] rowName,List<Object[]> dataList){
this.dataList = dataList;
this.rowName = rowName;
this.title = title;
}

/*
* 导出数据
* */
public void export() throws Exception{
try{
HSSFWorkbook workbook = new HSSFWorkbook(); // 创建工作簿对象
HSSFSheet sheet = workbook.createSheet(title); // 创建工作表

// 产生表格标题行
HSSFRow rowm = sheet.createRow(0);
HSSFCell cellTiltle = rowm.createCell(0);

//sheet样式定义【getColumnTopStyle()/getStyle()均为自定义方法 - 在下面 - 可扩展】
HSSFCellStyle columnTopStyle = this.getColumnTopStyle(workbook);//获取列头样式对象
HSSFCellStyle style = this.getStyle(workbook); //单元格样式对象

sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, (rowName.length-1)));
cellTiltle.setCellStyle(columnTopStyle);
cellTiltle.setCellValue(title);

// 定义所需列数
int columnNum = rowName.length;
HSSFRow rowRowName = sheet.createRow(2); // 在索引2的位置创建行(最顶端的行开始的第二行)

// 将列头设置到sheet的单元格中
for(int n=0;n<columnNum;n++){
HSSFCell cellRowName = rowRowName.createCell(n); //创建列头对应个数的单元格
cellRowName.setCellType(HSSFCell.CELL_TYPE_STRING); //设置列头单元格的数据类型
HSSFRichTextString text = new HSSFRichTextString(rowName[n]);
cellRowName.setCellValue(text); //设置列头单元格的值
cellRowName.setCellStyle(columnTopStyle); //设置列头单元格样式
}

//将查询出的数据设置到sheet对应的单元格中
for(int i=0;i<dataList.size();i++){

Object[] obj = dataList.get(i);//遍历每个对象
HSSFRow row = sheet.createRow(i+3);//创建所需的行数

for(int j=0; j<obj.length; j++){
HSSFCell cell = null; //设置单元格的数据类型
if(j == 0){
cell = row.createCell(j,HSSFCell.CELL_TYPE_NUMERIC);
cell.setCellValue(i+1);
}else{
cell = row.createCell(j,HSSFCell.CELL_TYPE_STRING);
if(!"".equals(obj[j]) && obj[j] != null){
cell.setCellValue(obj[j].toString()); //设置单元格的值
}
}
cell.setCellStyle(style); //设置单元格样式
}
}
//让列宽随着导出的列长自动适应
for (int colNum = 0; colNum < columnNum; colNum++) {
int columnWidth = sheet.getColumnWidth(colNum) / 256;
for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) {
HSSFRow currentRow;
//当前行未被使用过
if (sheet.getRow(rowNum) == null) {
currentRow = sheet.createRow(rowNum);
} else {
currentRow = sheet.getRow(rowNum);
}
if (currentRow.getCell(colNum) != null) {
HSSFCell currentCell = currentRow.getCell(colNum);
if (currentCell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
int length = currentCell.getStringCellValue().getBytes().length;
if (columnWidth < length) {
columnWidth = length;
}
}
}
}
if(colNum == 0){
sheet.setColumnWidth(colNum, (columnWidth-2) * 256);
}else{
sheet.setColumnWidth(colNum, (columnWidth+4) * 256);
}
}

if(workbook !=null){
try
{
String fileName = "Excel-" + String.valueOf(System.currentTimeMillis()).substring(4, 13) + ".xls";
String headStr = "attachment; filename=\"" + fileName + "\"";
response = getResponse();
response.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader("Content-Disposition", headStr);
OutputStream out = response.getOutputStream();
workbook.write(out);
}
catch (IOException e)
{
e.printStackTrace();
}
}

}catch(Exception e){
e.printStackTrace();
}

}

/*
* 列头单元格样式
*/
public HSSFCellStyle getColumnTopStyle(HSSFWorkbook workbook) {

// 设置字体
HSSFFont font = workbook.createFont();
//设置字体大小
font.setFontHeightInPoints((short)11);
//字体加粗
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//设置字体名字
font.setFontName("Courier New");
//设置样式;
HSSFCellStyle style = workbook.createCellStyle();
//设置底边框;
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
//设置底边框颜色;
style.setBottomBorderColor(HSSFColor.BLACK.index);
//设置左边框;
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
//设置左边框颜色;
style.setLeftBorderColor(HSSFColor.BLACK.index);
//设置右边框;
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
//设置右边框颜色;
style.setRightBorderColor(HSSFColor.BLACK.index);
//设置顶边框;
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
//设置顶边框颜色;
style.setTopBorderColor(HSSFColor.BLACK.index);
//在样式用应用设置的字体;
style.setFont(font);
//设置自动换行;
style.setWrapText(false);
//设置水平对齐的样式为居中对齐;
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//设置垂直对齐的样式为居中对齐;
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

return style;

}

/*
* 列数据信息单元格样式
*/
public HSSFCellStyle getStyle(HSSFWorkbook workbook) {
// 设置字体
HSSFFont font = workbook.createFont();
//设置字体大小
//font.setFontHeightInPoints((short)10);
//字体加粗
//font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//设置字体名字
font.setFontName("Courier New");
//设置样式;
HSSFCellStyle style = workbook.createCellStyle();
//设置底边框;
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
//设置底边框颜色;
style.setBottomBorderColor(HSSFColor.BLACK.index);
//设置左边框;
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
//设置左边框颜色;
style.setLeftBorderColor(HSSFColor.BLACK.index);
//设置右边框;
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
//设置右边框颜色;
style.setRightBorderColor(HSSFColor.BLACK.index);
//设置顶边框;
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
//设置顶边框颜色;
style.setTopBorderColor(HSSFColor.BLACK.index);
//在样式用应用设置的字体;
style.setFont(font);
//设置自动换行;
style.setWrapText(false);
//设置水平对齐的样式为居中对齐;
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//设置垂直对齐的样式为居中对齐;
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

return style;

}
}

⑨ 如何使用Java POI生成Excel表文件

()throwsIOException{
Stringpath="C:\poi2.xlsx";
XSSFWorkbookworkbook=newXSSFWorkbook();
XSSFSheetsheet=workbook.createSheet("我的Sheet");

XSSFRowrow=sheet.createRow(0);
XSSFCellcell=row.createCell(0);
cell.setCellValue("我是写入的");

XSSFRowrow1=sheet.createRow(1);
XSSFCellcell1=row1.createCell(0);
cell1.setCellValue("2010");

FileOutputStreamoutputStream=newFileOutputStream(path);
workbook.write(outputStream);
outputStream.close();
}

()throwsIOException{
Stringpath="C:\poi2.xls";
HSSFWorkbookworkbook=newHSSFWorkbook();
HSSFSheetsheet=workbook.createSheet("我的Excel");
HSSFRowrow=sheet.createRow(0);
HSSFCellcell=row.createCell(0);
cell.setCellValue("我是POI写入的");
FileOutputStreamoutputStream=newFileOutputStream(path);
workbook.write(outputStream);
outputStream.close();
}

⑩ java poi导出excel问题

是不是width设置不够,增加点长度试一试

阅读全文

与javapoi导出xls相关的资料

热点内容
江阴证件文件翻译多少钱 浏览:316
javaruntime142 浏览:610
word把数字变斜 浏览:372
小米6忘记锁屏密码怎么办啊 浏览:462
北京白领都用什么社交app 浏览:518
政法app哪个好用 浏览:514
房产平台如何推广新网站 浏览:701
u盘导文件总是中断 浏览:995
下载的招标文件打不开是为什么 浏览:356
都市美艳后宫 浏览:435
十部顶级古埃及电影 浏览:107
linux用户读写权限 浏览:936
少侠十七妻全文阅读 浏览:422
公主奴 浏览:856
k9d3 浏览:182
分卷阅读 玩武警少尉 浏览:44
知乎写小说入口 浏览:772
美国农场爱情片 浏览:709
主角一开始就长生不老 浏览:338
mike文件内容和输入不匹配 浏览:499

友情链接