用jexcel读写excel的.xls文件的例子
来源:互联网 发布:c盘保护软件 编辑:程序博客网 时间:2024/05/13 15:24
以前自己写过一篇关于制作excel报表的例子,有兴趣的朋友可以去看看:http://www.cnblogs.com/loveJava/archive/2011/11/08/2241115.html。下面我就分享一下别人的代码,希望这两种方法对你能够有所帮助:
有时候我们会需要做excel的报表,下面这个例子详细的给出了读写及定义样式的方法。
(jexcel下载)
package com.test;import java.io.File;import java.io.IOException;import jxl.Cell;import jxl.Sheet;import jxl.Workbook;import jxl.format.Border;import jxl.format.BorderLineStyle;import jxl.format.Colour;import jxl.read.biff.BiffException;import jxl.write.Label;import jxl.write.WritableCellFormat;import jxl.write.WritableFont;import jxl.write.WritableSheet;import jxl.write.WritableWorkbook;import jxl.write.WriteException;import jxl.write.biff.RowsExceededException;/** * * CopyRight (C) www.blogjava.net/ilovezmh All rights reserved. * <p> * * WuHan Inpoint Information Technology Development,Inc. * <p> * * Author zhu * <p> * * @version 1.0 2007-2-6 * * <p> * Base on : JDK1.5 * <p> * */public class JexcelSample {/** * 写excel文件 * */public void writeExc(File filename) {WritableWorkbook wwb = null;try {wwb = Workbook.createWorkbook(filename);} catch (Exception e) {e.printStackTrace();}// 创建Excel工作表WritableSheet ws = wwb.createSheet("通讯录", 0);// 创建sheettry {ws.mergeCells(0, 0, 2, 1);// 合并单元格(左列,左行,右列,右行)从第1行第1列到第2行第3列Label header = new Label(0, 0, "通讯录(191026班)", getHeader());ws.addCell(header);// 写入头Label l = new Label(0, 2, "姓名", getTitle());// 第3行ws.addCell(l);l = new Label(1, 2, "电话", getTitle());ws.addCell(l);l = new Label(2, 2, "地址", getTitle());ws.addCell(l);l = new Label(0, 3, "小祝", getNormolCell());// 第4行ws.addCell(l);l = new Label(1, 3, "1314***0974", getNormolCell());ws.addCell(l);l = new Label(2, 3, "武汉武昌", getNormolCell());ws.addCell(l);l = new Label(0, 4, "小施", getNormolCell());// 第5行ws.addCell(l);l = new Label(1, 4, "1347***5057", getNormolCell());ws.addCell(l);l = new Label(2, 4, "武汉武昌", getNormolCell());ws.addCell(l);ws.setColumnView(0, 20);// 设置列宽ws.setColumnView(1, 20);ws.setColumnView(2, 40);ws.setRowView(0, 400);// 设置行高ws.setRowView(1, 400);ws.setRowView(2, 500);ws.setRowView(3, 500);ws.setRowView(4, 500);} catch (RowsExceededException e1) {e1.printStackTrace();} catch (WriteException e1) {e1.printStackTrace();}// 输出流try {wwb.write();} catch (IOException ex) {// TODO 自动生成 catch 块ex.printStackTrace();}// 关闭流try {wwb.close();} catch (WriteException ex) {// TODO 自动生成 catch 块ex.printStackTrace();} catch (IOException ex) {// TODO 自动生成 catch 块ex.printStackTrace();}// outStream.close();System.out.println("写入成功!\n");}public void readExc(File filename) throws BiffException, IOException {Workbook wb = Workbook.getWorkbook(filename);Sheet s = wb.getSheet(0);// 第1个sheetCell c = null;int row = s.getRows();// 总行数int col = s.getColumns();// 总列数for (int i = 0; i < row; i++) {for (int j = 0; j < col; j++) {c = s.getCell(j, i);System.out.print(c.getContents() + " ");}System.out.println();}}/** * 设置头的样式 * * @return */public static WritableCellFormat getHeader() {WritableFont font = new WritableFont(WritableFont.TIMES, 24,WritableFont.BOLD);// 定义字体try {font.setColour(Colour.BLUE);// 蓝色字体} catch (WriteException e1) {// TODO 自动生成 catch 块e1.printStackTrace();}WritableCellFormat format = new WritableCellFormat(font);try {format.setAlignment(jxl.format.Alignment.CENTRE);// 左右居中format.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);// 上下居中format.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);// 黑色边框format.setBackground(Colour.YELLOW);// 黄色背景} catch (WriteException e) {// TODO 自动生成 catch 块e.printStackTrace();}return format;}/** * 设置标题样式 * * @return */public static WritableCellFormat getTitle() {WritableFont font = new WritableFont(WritableFont.TIMES, 14);try {font.setColour(Colour.BLUE);// 蓝色字体} catch (WriteException e1) {// TODO 自动生成 catch 块e1.printStackTrace();}WritableCellFormat format = new WritableCellFormat(font);try {format.setAlignment(jxl.format.Alignment.CENTRE);format.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);format.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);} catch (WriteException e) {// TODO 自动生成 catch 块e.printStackTrace();}return format;}/** * 设置其他单元格样式 * * @return */public static WritableCellFormat getNormolCell() {// 12号字体,上下左右居中,带黑色边框WritableFont font = new WritableFont(WritableFont.TIMES, 12);WritableCellFormat format = new WritableCellFormat(font);try {format.setAlignment(jxl.format.Alignment.CENTRE);format.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);format.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);} catch (WriteException e) {// TODO 自动生成 catch 块e.printStackTrace();}return format;}public static void main(String[] args) throws IOException, BiffException {JexcelSample js = new JexcelSample();File f = new File("D:\\address.xls");f.createNewFile();js.writeExc(f);js.readExc(f);}}
生成的excel表格如下:
- 用jexcel读写excel的.xls文件的例子
- 用jexcel读写excel的.xls文件的例子
- 用jexcel读写excel的.xls文件的例子 带样式
- 使用jexcel读写excel文件的例子
- 用jxl读写excel的.xls文件的例子 带样式
- 用jxl读写excel的.xls文件的例子 带样式
- 用jxl读写excel的.xls文件的例子 带样式
- jexcel 导出excel文件的demo
- XLS文件的读写
- jexcel 读写EXCEL
- JAVA 读写 Excel--jexcel
- 【原创】.NET读写Excel工具Spire.Xls使用(2)Excel文件的控制
- 关于:读写xls文件,提供下载的一个小例子(jxl 的简单运用)
- matlab中读取excel的xls文件
- jxl导出xls后缀的excel文件
- OpenXML读写EXCEL的例子
- 使用JExcel解析Excel文件
- 使用JExcel解析Excel文件
- C#聊天机器人
- 内存分配在栈上创建与堆上分配的区别
- Windows远程桌面(xrdp+vnc)连接Ubuntu
- 喜欢偶数的Convert.ToInt32(double)
- 【Linux的高级应用编程】Linux下的线程实现机制分析
- 用jexcel读写excel的.xls文件的例子
- 架设WIN32汇编程序的开发环境
- 收集的精典php代码
- 【Linux的高级应用编程】Linux下的多线程编程
- 指针做形参
- 自定义intent action
- 【Linux的高级应用编程】Linux下的多进程编程
- Linux动态链接库搜索路径问题
- 学会用core dump调试程序错误