现加个jar包

http://pan.baidu.com/s/1boe5kXh   vfp8

然后代码

package makeReportToExcel;

import java.io.File;
import java.io.IOException;
import java.util.Locale; import jxl.CellView;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.format.UnderlineStyle;
import jxl.write.Formula;
import jxl.write.Label;
import jxl.write.Number;
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;
public class WriteExcel { private WritableCellFormat timesBoldUnderline;
private WritableCellFormat times;
private String inputFile; public void setOutputFile(String inputFile) {
this.inputFile = inputFile;
} public void write() throws IOException, WriteException {
File file = new File(inputFile);
WorkbookSettings wbSettings = new WorkbookSettings(); wbSettings.setLocale(new Locale("en", "EN")); WritableWorkbook workbook = Workbook.createWorkbook(file, wbSettings);
workbook.createSheet("Report", 0);
WritableSheet excelSheet = workbook.getSheet(0);
createLabel(excelSheet);
createContent(excelSheet); workbook.write();
workbook.close();
} private void createLabel(WritableSheet sheet)
throws WriteException {
// Lets create a times font
WritableFont times10pt = new WritableFont(WritableFont.TIMES, 10);
// Define the cell format
times = new WritableCellFormat(times10pt);
// Lets automatically wrap the cells
times.setWrap(true); // create create a bold font with unterlines
WritableFont times10ptBoldUnderline = new WritableFont(WritableFont.TIMES, 10, WritableFont.BOLD, false,
UnderlineStyle.SINGLE);
timesBoldUnderline = new WritableCellFormat(times10ptBoldUnderline);
// Lets automatically wrap the cells
timesBoldUnderline.setWrap(true); CellView cv = new CellView();
cv.setFormat(times);
cv.setFormat(timesBoldUnderline);
cv.setAutosize(true); // Write a few headers
addCaption(sheet, 0, 0, "Header 1");
addCaption(sheet, 1, 0, "This is another header"); } private void createContent(WritableSheet sheet) throws WriteException,
RowsExceededException {
// Write a few number
for (int i = 1; i < 10; i++) {
// First column
addNumber(sheet, 0, i, i + 10);
// Second column
addNumber(sheet, 1, i, i * i);
}
// Lets calculate the sum of it
StringBuffer buf = new StringBuffer();
buf.append("SUM(A2:A10)");
Formula f = new Formula(0, 10, buf.toString());
sheet.addCell(f);
buf = new StringBuffer();
buf.append("SUM(B2:B10)");
f = new Formula(1, 10, buf.toString());
sheet.addCell(f); // now a bit of text
for (int i = 12; i < 20; i++) {
// First column
addLabel(sheet, 0, i, "Boring text " + i);
// Second column
addLabel(sheet, 1, i, "Another text");
}
} private void addCaption(WritableSheet sheet, int column, int row, String s)
throws RowsExceededException, WriteException {
Label label;
label = new Label(column, row, s, timesBoldUnderline);
sheet.addCell(label);
} private void addNumber(WritableSheet sheet, int column, int row,
Integer integer) throws WriteException, RowsExceededException {
Number number;
number = new Number(column, row, integer, times);
sheet.addCell(number);
} private void addLabel(WritableSheet sheet, int column, int row, String s)
throws WriteException, RowsExceededException {
Label label;
label = new Label(column, row, s, times);
sheet.addCell(label);
} public static void main(String[] args) throws WriteException, IOException {
WriteExcel test = new WriteExcel();
test.setOutputFile("c:/temp/lars.xls");
test.write();
System.out
.println("Please check the result file under c:/temp/lars.xls ");
}
}

下面的代码是从excel中读出内容

package makeReportToExcel;

import java.io.File;
import java.io.IOException; import jxl.Cell;
import jxl.CellType;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException; public class ReadExcel { private String inputFile; public void setInputFile(String inputFile) {
this.inputFile = inputFile;
} public void read() throws IOException {
File inputWorkbook = new File(inputFile);
Workbook w;
try {
w = Workbook.getWorkbook(inputWorkbook);
// Get the first sheet
Sheet sheet = w.getSheet(0);
// Loop over first 10 column and lines for (int j = 0; j < sheet.getColumns(); j++) {
for (int i = 0; i < sheet.getRows(); i++) {
Cell cell = sheet.getCell(j, i);
CellType type = cell.getType();
if (type == CellType.LABEL) {
System.out.println("I got a label "
+ cell.getContents());
} if (type == CellType.NUMBER) {
System.out.println("I got a number "
+ cell.getContents());
} }
}
} catch (BiffException e) {
e.printStackTrace();
}
} public static void main(String[] args) throws IOException {
ReadExcel test = new ReadExcel();
test.setInputFile("c:/temp/lars.xls");
test.read();
} }

java 报表到excel的更多相关文章

  1. Java报表工具FineReport导出EXCEL的四种API

    在实际的应用中会经常需要将数据导出成excel,导出的方式除原样导出还有分页导出.分页分sheet导出和大数据量导出.对于excel 2003版,由于限制了每个sheet的最大行数和列数,大数据量导出 ...

  2. java读取大容量excel之二(空格、空值问题)

    最近在项目中发现,对于Excel2007(底层根本是xml) ,使用<java读取大容量excel之一>中的方式读取,若待读取的excel2007文件中某一列是空值,(注意,所谓的空值是什 ...

  3. Java报表开发组件DynamicReports

    DynamicReports 是一个基于 JasperReports 进行扩展的 Java 报表库,可用它来快速创建报表而无需可视化报表设计工具. From :  http://www.oschina ...

  4. FineReport实现Java报表主题分析的效果图

    Java报表-財务主题-EVA经济附加 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYmVzdF9yZXBvcnQ=/font/5a6L5L2T/font ...

  5. 重构:以Java POI 导出EXCEL为例

    重构 开头先抛出几个问题吧,这几个问题也是<重构:改善既有代码的设计>这本书第2章的问题. 什么是重构? 为什么要重构? 什么时候要重构? 接下来就从这几个问题出发,通过这几个问题来系统的 ...

  6. Java对象和Excel转换工具XXL-EXCEL

    <Java对象和Excel转换工具XXL-EXCEL> 一.简介 1.1 概述 XXL-EXCEL 是一个灵活的Java对象和Excel文档相互转换的工具. 一行代码完成Java对象和Ex ...

  7. FineReport实现java报表权限使用的效果图

    Java报表-多级权限配置说明 Java报表-联合填报 Java报表-模板内容权限控制 Java报表-权限细粒度控制

  8. java POI创建Excel示例(xslx和xsl区别 )

    Java用来处理office类库有很多,其中POI就是比较出名的一个,它是apache的类库,现在版本到了3.10,也就是2014年2月8号这个版本. 在处理PPT,Excel和Word前,需要导入以 ...

  9. 葡萄城报表介绍:Java 报表

    一.Java 报表定义 Java 是一门面向对象编程语言,不仅吸收了 C++ 语言的各种优点,还摒弃了 C++ 里难以理解的多继承.指针等概念,因此 Java 语言具有功能强大和简单易用两个特征.Ja ...

随机推荐

  1. 通过SqlClr制作Sql自动化批量执行脚本

    原文:通过SqlClr制作Sql自动化批量执行脚本 通过SqlClr制作Sql自动化批量执行脚本 在与同事一起做项目时,看到同事用sqlclr做批量执行脚本,感觉挺新奇的就上网搜集资料自己模仿跟做了个 ...

  2. java在string和int相互转化

    1 如何串 String 转换成整数 int? A. 有两种方法: 1). int i = Integer.parseInt([String]); 或 i = Integer.parseInt([St ...

  3. 【工作笔记五】html5的placeholder属性(IE如何兼容placeholder属性)

    placeholder属性,IE对其的支持表示无奈,firefox.google chrome表示毫无压力. HTML5对Web Form做了许多增强,比如input新增的type类型.Form Va ...

  4. ASP.NET页面生命周期和asp.net应用程序生命周期

    作为一个asp.net程序员,不了解ASP.NET页面生命周期和asp.net应用程序生命周期是绝对不行的,那永远只能是菜鸟级别.我很早就学过,可是没次还是得去翻,一些事件还是记不住,还是记在自己的b ...

  5. php和cookie

    cookie常用于用户识别,是服务器留在用户计算机中的小文件. cookie在浏览器端和服务器端的通信过程大致是这样: 浏览器向服务器作出请求(如果浏览器有cookie,将在request heade ...

  6. javascript this指向

    this对象是什么: this对象是与运行时函数执行的上下文绑定的.这句话其实已经很好的解释了this对象,为我们确定this指明了方向!但是需要注意的是:由于javascript具有动态性(解释执行 ...

  7. jquery简单异步读取xml文件

    $.ajax({            url: '../XmlFiles/Sm.xml',            async: true,            cache: false,      ...

  8. JavaScript实例技巧精选(12)—计算星座与属相

    >>点击这里下载完整html源码<< 这是截图: 核心代码如下: <SCRIPT LANGUAGE="JavaScript"> <!-- ...

  9. vs2012快速将项目托管到github

    vs2012快速将项目托管到github   在VS2012中使用GitHub 注册GitHub账号(DeanZhouLin) https://github.com/ 向GitHub中添加一个仓库(T ...

  10. Javascript模块化编程之难处

    接着上一篇“Javascript模块化编程之Why”说起,Javascript担子重了之后程序也就复杂了.在大把语言都模块化编程的形势下,Javascript也不可能袖手旁观啊,毕竟这是一条经过实践检 ...