当前位置:首页 » 办公资讯 » java怎样生成excel

java怎样生成excel

发布时间: 2022-01-10 04:37:24

㈠ java如何实现生成自定义excel表格,如下图,

使用excel编好,再用jxl或poi填入数据

㈡ java怎么生成excel文件工具类

packagebeans.excel;

importjava.io.IOException;
importjava.io.OutputStream;

importjxl.Workbook;
importjxl.write.Label;
importjxl.write.WritableSheet;
importjxl.write.WritableWorkbook;
importjxl.write.WriteException;

publicclassSimpleExcelWrite{
publicvoidcreateExcel(OutputStreamos)throwsWriteException,IOException{
//创建工作薄
WritableWorkbookworkbook=Workbook.createWorkbook(os);
//创建新的一页
WritableSheetsheet=workbook.createSheet("FirstSheet",0);
//创建要显示的内容,创建一个单元格,第一个参数为列坐标,第二个参数为行坐标,第三个参数为内容
Labelxuexiao=newLabel(0,0,"学校");
sheet.addCell(xuexiao);
Labelzhuanye=newLabel(1,0,"专业");
sheet.addCell(zhuanye);
Labeljingzhengli=newLabel(2,0,"专业竞争力");
sheet.addCell(jingzhengli);

Labelqinghua=newLabel(0,1,"清华大学");
sheet.addCell(qinghua);
Labeljisuanji=newLabel(1,1,"计算机专业");
sheet.addCell(jisuanji);
Labelgao=newLabel(2,1,"高");
sheet.addCell(gao);

Labelbeida=newLabel(0,2,"北京大学");
sheet.addCell(beida);
Labelfalv=newLabel(1,2,"法律专业");
sheet.addCell(falv);
Labelzhong=newLabel(2,2,"中");
sheet.addCell(zhong);

Labelligong=newLabel(0,3,"北京理工大学");
sheet.addCell(ligong);
Labelhangkong=newLabel(1,3,"航空专业");
sheet.addCell(hangkong);
Labeldi=newLabel(2,3,"低");
sheet.addCell(di);

//把创建的内容写入到输出流中,并关闭输出流
workbook.write();
workbook.close();
os.close();
}

}

㈢ java怎么生成EXCEL文件

可以使用apache的poi包,这是一个开源项目,可方便读写excel文件,这里有一些例子:
http://dev.csdn.net/develop/article/73/73804.shtm
http://www.chinahtm.cn/program/26385.html

㈣ 如何在java中做excel表

查看POI的使用文档和api吧,貌似没有比它更强的api工具包了

㈤ java代码怎么导出excel文件

excel工具类
package com.ohd.ie.proct.action;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import org.apache.commons.io.output.ByteArrayOutputStream;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.VerticalAlignment;
import jxl.write.*;
import jxl.write.Number;
import jxl.write.biff.RowsExceededException;
public class Excel {
private OutputStream os;
private WritableWorkbook wwb = null;
private WritableSheet ws = null;
private WritableCellFormat titleCellFormat = null;
private WritableCellFormat noBorderCellFormat = null;
private WritableCellFormat hasBorderCellFormat = null;
private WritableCellFormat hasBorderCellNumberFormat = null;
private WritableCellFormat hasBorderCellNumberFormat2 = null;
private WritableImage writableImage=null;
private int r;
public Excel(OutputStream os){
this.os = os;
r = -1;
try {
wwb = Workbook.createWorkbook(os);
//创建工作表
ws = wwb.createSheet("sheet1",0);
//设置表头字体,大小,加粗
titleCellFormat = new WritableCellFormat();
titleCellFormat.setAlignment(Alignment.CENTRE);
titleCellFormat.setVerticalAlignment(VerticalAlignment.CENTRE);
//自动换行
titleCellFormat.setWrap(true);
titleCellFormat.setFont(new WritableFont(WritableFont.createFont("宋体"),12,WritableFont.BOLD));
titleCellFormat.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
//设置表格字体,大小----无边框
noBorderCellFormat = new WritableCellFormat();
noBorderCellFormat.setAlignment(Alignment.CENTRE);
noBorderCellFormat.setVerticalAlignment(VerticalAlignment.CENTRE);
noBorderCellFormat.setFont(new WritableFont(WritableFont.createFont("宋体"),12));
//设置表格字体,大小----有边框
hasBorderCellFormat = new WritableCellFormat();
hasBorderCellFormat.setAlignment(Alignment.CENTRE);
hasBorderCellFormat.setVerticalAlignment(VerticalAlignment.CENTRE);
hasBorderCellFormat.setFont(new WritableFont(WritableFont.createFont("宋体"),12));
hasBorderCellFormat.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
//设置表格字体,大小----有边框(小数)
NumberFormat nf = new NumberFormat("#0.00");
hasBorderCellNumberFormat = new WritableCellFormat(nf);
hasBorderCellNumberFormat.setAlignment(Alignment.CENTRE);
hasBorderCellNumberFormat.setVerticalAlignment(VerticalAlignment.CENTRE);
hasBorderCellNumberFormat.setFont(new WritableFont(WritableFont.createFont("宋体"),12));
hasBorderCellNumberFormat.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
//设置表格字体,大小----有边框(整数)
NumberFormat nf2 = new NumberFormat("#0");
hasBorderCellNumberFormat2 = new WritableCellFormat(nf2);
hasBorderCellNumberFormat2.setAlignment(Alignment.CENTRE);
hasBorderCellNumberFormat2.setVerticalAlignment(VerticalAlignment.CENTRE);
hasBorderCellNumberFormat2.setFont(new WritableFont(WritableFont.createFont("宋体"),12));
hasBorderCellNumberFormat2.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
*
* @param content 内容
* @param c 列
* @param style 样式
* @param isNewLine 是否换行
* @param mergeType 合并类型
* @param mergeCount 合并个数
* @param width 单元格宽
*/
public void setExcelCell(String content,int c,int style,boolean isNewLine,int mergeType,int mergeCount,int width){
try {
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////报表内容////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
if(isNewLine){
r++;
}
WritableCell l = null;
if(style == 1){
l = new Label(c,r,content,titleCellFormat);
}
else if(style == 2){
l = new Label(c,r,content,noBorderCellFormat);
}
else if(style == 3){
l = new Label(c,r,content,hasBorderCellFormat);
}
else if(style == 4){
l = new Number(c,r,Double.parseDouble(content),hasBorderCellNumberFormat);
}
else if(style == 5){
l = new Number(c,r,Integer.parseInt(content),hasBorderCellNumberFormat2);
}
ws.addCell(l);
if(width != 0){
ws.setColumnView(c,width);
}
//veryhuo,com
if(mergeType == 1){
//x 轴方向
ws.mergeCells(c, r, c+mergeCount-1 , r);
}
else if(mergeType == 2){
//y 轴方向
ws.mergeCells(c, r, c, r+mergeCount-1);
}
if(isNewLine){
ws.setRowView(r, 350);
if(style == 1 && r != 0){
ws.setRowView(r, 900);
}
else{
ws.setRowView(r, 350);
}
}
//
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
} catch (Exception e) {
System.out.println(e.toString());
}
}
public void setExcelCellEx(String content,int c,int style,boolean isNewLine,int mergeType,int mergeCount,int width,int row){
try {
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////报表内容////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
if(isNewLine){
r++;
}
WritableCell l = null;
if(style == 1){
l = new Label(c,r,content,titleCellFormat);
}
else if(style == 2){
l = new Label(c,r,content,noBorderCellFormat);
}
else if(style == 3){
if(content.indexOf(".jpg")!=-1 ||content.indexOf(".JPG")!=-1){
File outputFile=null;
File imgFile =new File(content);
if(imgFile.exists()&&imgFile.length()>0){
BufferedImage input=null;
try {
input = ImageIO.read(imgFile);
} catch (Exception e) {
e.printStackTrace();
}
if(input!=null){
String path=imgFile.getAbsolutePath();
outputFile = new File(path.substring(0,path.lastIndexOf('.')+1)+"png");
ImageIO.write(input, "PNG", outputFile);
if(outputFile.exists()&&outputFile.length()>0){
ws.setRowView(row,2000);
//ws.setColumnView(8, 10);
writableImage = new WritableImage(c+0.1, row+0.1, 0.8, 0.8, outputFile);
ws.addImage(writableImage);
l = new Label(c,r,"",hasBorderCellFormat);
}
}
}
}else{
l = new Label(c,r,content,hasBorderCellFormat);
}
}
else if(style == 4){
l = new Number(c,r,Double.parseDouble(content),hasBorderCellNumberFormat);
}
else if(style == 5){
l = new Number(c,r,Integer.parseInt(content),hasBorderCellNumberFormat2);
}
ws.addCell(l);
if(width != 0){
ws.setColumnView(c,width);
}
if(mergeType == 1){
//x 轴方向
ws.mergeCells(c, r, c+mergeCount-1 , r);
}
else if(mergeType == 2){
//y 轴方向
ws.mergeCells(c, r, c, r+mergeCount-1);
}
if(isNewLine){
ws.setRowView(r, 350);
if(style == 1 && r != 0){
ws.setRowView(r, 900);
}
else{
ws.setRowView(r, 350);
}
}
} catch (Exception e) {
System.out.println(e.toString());
}
}
public void setRowHeight(int val){
try {
ws.setRowView(r, val);
} catch (RowsExceededException e) {
e.printStackTrace();
}
}
public void getExcelResult(){
try {
wwb.write();
} catch (Exception e) {
System.out.println(e.toString());
}
finally{
if(wwb != null){
try {
wwb.close();
if(os != null){
os.close();
}
} catch (WriteException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
需要的jar包:jxl.jar

㈥ 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数据怎么导出到excel

如果是导出成表格,使用,分隔的csv文件就方便,,,,,,,否则,使用POI导出成EXCEL格式的
~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~
~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~

㈧ 如何用JAVA导出Excel(使用POI)

// 创建临时文件(excel为Workbook对象)
response.reset();
response.setContentType("application/download");
response.setHeader("Content-Disposition",
"attachment;filename=" + excel.getFileName());
excel.getExcel().write(response.getOutputStream());
response.flushBuffer();

㈨ java中怎么生成excel表格 csdn

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

㈩ 利用java怎么实现生成报表(Excel文件)

JAVA POI 组件//创建HSSFWorkbook对象
HSSFWorkbook wb = new HSSFWorkbook();
//创建HSSFSheet对象
HSSFSheet sheet = wb.createSheet("sheet0");
//创建HSSFRow对象
HSSFRow row = sheet.createRow((short)0);
//创建HSSFCell对象
HSSFCell cell=row.createCell((short)0);
//用来处理中文问题
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
//设置单元格的值
cell.setCellValue("单元格中的中文");
//定义你需要的输出流
OutputStream out = new FileOutputStream("viwo.xls");
//输出Excel

热点内容
马路上汽车的噪音在多少分贝 发布:2023-08-31 22:08:23 浏览:1853
应孕棒多少钱一盒 发布:2023-08-31 22:08:21 浏览:1325
标准养老金一年能领多少钱 发布:2023-08-31 22:05:05 浏览:1612
湖北通城接网线多少钱一个月 发布:2023-08-31 21:59:51 浏览:1691
开随车吊车多少钱一个月 发布:2023-08-31 21:55:06 浏览:1455
京东付尾款怎么知道前多少名 发布:2023-08-31 21:52:58 浏览:1777
在学校租铺面一个月要多少钱 发布:2023-08-31 21:52:09 浏览:1911
2寸有多少厘米 发布:2023-08-31 21:50:34 浏览:1559
知道电压如何算一小时多少电 发布:2023-08-31 21:46:20 浏览:1535
金手镯54号圈周长是多少厘米 发布:2023-08-31 21:44:28 浏览:1712