本文目录一览:

怎么用java实现导出excel

/**

 * @author liuwu

 * Excel的导入与导出

 */

@SuppressWarnings({ "unchecked" })

public class ExcelOperate {

/**

 * @author liuwu

 * 这是一个通用的方法,利用了JAVA的反射机制,

 * 可以将放置在JAVA集合中并且符合一定条件的数据以EXCEL的形式输出到指定IO设备上

 * @param title 表格标题名

 * @param headers 表格属性列名数组

 * @param dataset 需要显示的数据集合,集合中一定要放置符合javabean风格的类的对象。

 *  此方法支持的 javabean属性【数据类型有java基本数据类型及String,Date,byte[](图片转成字节码)】

 * @param out 与输出设备关联的流对象,可以将EXCEL文档导出到本地文件或者网络中

 * @param pattern  如果有时间数据,设定输出格式。默认为"yyy-MM-dd"

 * @throws IOException 

 */

public static void exportExcel(String title, String[] headers,Collection? dataset, OutputStream out, String pattern) throws IOException {

// 声明一个工作薄

HSSFWorkbook workbook = new HSSFWorkbook();

// 生成一个表格

HSSFSheet sheet = workbook.createSheet(title);

// 设置表格默认列宽度为15个字节

sheet.setDefaultColumnWidth((short) 20);

// 生成一个样式

HSSFCellStyle style = workbook.createCellStyle();

// 设置这些样式

style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);

style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

style.setBorderBottom(HSSFCellStyle.BORDER_THIN);

style.setBorderLeft(HSSFCellStyle.BORDER_THIN);

style.setBorderRight(HSSFCellStyle.BORDER_THIN);

style.setBorderTop(HSSFCellStyle.BORDER_THIN);

style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

// 生成一个字体

HSSFFont font = workbook.createFont();

font.setColor(HSSFColor.VIOLET.index);

font.setFontHeightInPoints((short) 12);

font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

// 把字体应用到当前的样式

style.setFont(font);

// 生成并设置另一个样式

HSSFCellStyle style2 = workbook.createCellStyle();

style2.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);

style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);

style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);

style2.setBorderRight(HSSFCellStyle.BORDER_THIN);

style2.setBorderTop(HSSFCellStyle.BORDER_THIN);

style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);

style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

// 生成另一个字体

HSSFFont font2 = workbook.createFont();

font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);

// 把字体应用到当前的样式

style2.setFont(font2);

// 产生表格标题行

HSSFRow row = sheet.createRow(0);

for (short i = 0; i  headers.length; i++) {

HSSFCell cell = row.createCell(i);

cell.setCellStyle(style);

HSSFRichTextString text = new HSSFRichTextString(headers[i]);

cell.setCellValue(text);

}

// 遍历集合数据,产生数据行

Iterator? it = dataset.iterator();

int index = 0;

while (it.hasNext()) {

index++;

row = sheet.createRow(index);

Object t =  it.next();

// 利用反射,根据javabean属性的先后顺序,动态调用getXxx()方法得到属性值

Field[] fields = t.getClass().getDeclaredFields();

for (short i = 0; i  fields.length; i++) {

HSSFCell cell = row.createCell(i);

cell.setCellStyle(style2);

Field field = fields[i];

String fieldName = field.getName();

String getMethodName = "get"

+ fieldName.substring(0, 1).toUpperCase()

+ fieldName.substring(1);//注意 实体get Set不要自己改名字不然反射会有问题

try {

Class tCls = t.getClass();

Method getMethod = tCls.getMethod(getMethodName,new Class[] {});

Object value = getMethod.invoke(t, new Object[] {});

HSSFRichTextString richString = new HSSFRichTextString(value.toString());

HSSFFont font3 = workbook.createFont();

font3.setColor(HSSFColor.BLUE.index);

richString.applyFont(font3);

cell.setCellValue(richString);

} catch (SecurityException e) {

e.printStackTrace();

e=null;

} catch (NoSuchMethodException e) {

e.printStackTrace();

e=null;

} catch (IllegalArgumentException e) {

e.printStackTrace();

e=null;

} catch (IllegalAccessException e) {

e.printStackTrace();

e=null;

} catch (InvocationTargetException e) {

e.printStackTrace();

e=null;

} finally {

// 清理资源

}

}

}

try {

workbook.write(out);

} catch (IOException e) {

e.printStackTrace();

e=null;

}

}

}

java怎么导出excel表格

通过这个例子,演示以下如何用java生成excel文件:

import org.apache.poi.hssf.usermodel.*;

import java.io.FileOutputStream;

import java.io.IOException;

publicclass CreateCells

{

publicstaticvoid main(String[] args)

throws IOException

{

HSSFWorkbook wb = new HSSFWorkbook();//建立新HSSFWorkbook对象

HSSFSheet sheet = wb.createSheet("new sheet");//建立新的sheet对象

// Create a row and put some cells in it. Rows are 0 based.

HSSFRow row = sheet.createRow((short)0);//建立新行

// Create a cell and put a value in it.

HSSFCell cell = row.createCell((short)0);//建立新cell

cell.setCellValue(1);//设置cell的整数类型的值

// Or do it on one line.

row.createCell((short)1).setCellValue(1.2);//设置cell浮点类型的值

row.createCell((short)2).setCellValue("test");//设置cell字符类型的值

row.createCell((short)3).setCellValue(true);//设置cell布尔类型的值

HSSFCellStyle cellStyle = wb.createCellStyle();//建立新的cell样式

cellStyle.setDataFormat(HSSFDataFormat.getFormat("m/d/yy h:mm"));//设置cell样式为定制的日期格式

HSSFCell dCell =row.createCell((short)4);

dCell.setCellValue(new Date());//设置cell为日期类型的值

dCell.setCellStyle(cellStyle); //设置该cell日期的显示格式

HSSFCell csCell =row.createCell((short)5);

csCell.setEncoding(HSSFCell.ENCODING_UTF_16);//设置cell编码解决中文高位字节截断

csCell.setCellValue("中文测试_Chinese Words Test");//设置中西文结合字符串

row.createCell((short)6).setCellType(HSSFCell.CELL_TYPE_ERROR);//建立错误cell

// Write the output to a file

FileOutputStream fileOut = new FileOutputStream("workbook.xls");

wb.write(fileOut);

fileOut.close();

}

}

java导出excel

java导出Excel java 代码 /* * Generated by MyEclipse Struts * Template path: templates/java/JavaClass.vtl */ package com.axon.fable.sams.view.action; import java.io.IOException; import java.io.OutputStream; import java.util.List; import javax.serv ... java导出Excel例举方式 方法一:导出Excel数据的插件jexcelapi 程序实例如下: public void exportClassroom(OutputStream os) throws PaikeException { try { WritableWorkbook wbook = Workbook.createWorkbook(os); //建立excel文件 WritableSheet wsheet = wbook.createSheet("教室信息表", 0); //工作表名称 //设置Excel字体 WritableFont wfont = new WritableFont(WritableFont.ARIAL, 16, WritableFont.BOLD, false, jxl.format.UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK); WritableCellFormat titleFormat = new WritableCellFormat(wfont); String[] title = { "教室名", "容 量", "类 型", "其他说明" }; //设置Excel表头 for (int i = 0; i title.length; i++) { Label excelTitle = new Label(i, 0, title[i], titleFormat); wsheet.addCell(excelTitle); } int c = 1; //用于循环时Excel的行号 ClassroomService cs = new ClassroomService(); List list = cs.findAllClassroom(); //这个是从数据库中取得要导出的数据 Iterator it = list.iterator(); while (it.hasNext()) { ClassroomDTO crdto = (ClassroomDTO) it.next(); Label content1 = new Label(0, c, crdto.getRoomname()); Label content2 = new Label(1, c, crdto.getCapicity().toString()); Label content3 = new Label(2, c, crdto.getRoomTypeId() .toString()); Label content4 = new Label(3, c, crdto.getRemark()); wsheet.addCell(content1); wsheet.addCell(content2); wsheet.addCell(content3); wsheet.addCell(content4); c++; } wbook.write(); //写入文件 wbook.close(); os.close(); } catch (Exception e) { throw new PaikeException("导出文件出错"); } } 方法二:直接用Java代码实现导出Excel报表 /* * Generated by MyEclipse Struts * Template path: templates/java/JavaClass.vtl */