java 报表到excel
现加个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的更多相关文章
- Java报表工具FineReport导出EXCEL的四种API
在实际的应用中会经常需要将数据导出成excel,导出的方式除原样导出还有分页导出.分页分sheet导出和大数据量导出.对于excel 2003版,由于限制了每个sheet的最大行数和列数,大数据量导出 ...
- java读取大容量excel之二(空格、空值问题)
最近在项目中发现,对于Excel2007(底层根本是xml) ,使用<java读取大容量excel之一>中的方式读取,若待读取的excel2007文件中某一列是空值,(注意,所谓的空值是什 ...
- Java报表开发组件DynamicReports
DynamicReports 是一个基于 JasperReports 进行扩展的 Java 报表库,可用它来快速创建报表而无需可视化报表设计工具. From : http://www.oschina ...
- FineReport实现Java报表主题分析的效果图
Java报表-財务主题-EVA经济附加 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYmVzdF9yZXBvcnQ=/font/5a6L5L2T/font ...
- 重构:以Java POI 导出EXCEL为例
重构 开头先抛出几个问题吧,这几个问题也是<重构:改善既有代码的设计>这本书第2章的问题. 什么是重构? 为什么要重构? 什么时候要重构? 接下来就从这几个问题出发,通过这几个问题来系统的 ...
- Java对象和Excel转换工具XXL-EXCEL
<Java对象和Excel转换工具XXL-EXCEL> 一.简介 1.1 概述 XXL-EXCEL 是一个灵活的Java对象和Excel文档相互转换的工具. 一行代码完成Java对象和Ex ...
- FineReport实现java报表权限使用的效果图
Java报表-多级权限配置说明 Java报表-联合填报 Java报表-模板内容权限控制 Java报表-权限细粒度控制
- java POI创建Excel示例(xslx和xsl区别 )
Java用来处理office类库有很多,其中POI就是比较出名的一个,它是apache的类库,现在版本到了3.10,也就是2014年2月8号这个版本. 在处理PPT,Excel和Word前,需要导入以 ...
- 葡萄城报表介绍:Java 报表
一.Java 报表定义 Java 是一门面向对象编程语言,不仅吸收了 C++ 语言的各种优点,还摒弃了 C++ 里难以理解的多继承.指针等概念,因此 Java 语言具有功能强大和简单易用两个特征.Ja ...
随机推荐
- iScroll 4.2.5 中文API
概况 资料来源 http://cubiq.org/iscroll-4 http://www.cnblogs.com/wanghun/archive/2012/10/17/2727416.html ht ...
- Android项目----dispathTouchEvent
说到dispathTouchEvent,就不得不说一个最贱的屏幕触摸动作触发的一些列Touch事件: ACTION_DOWN->ACTION_MOVE->ACTION_MOVE->A ...
- AngularJS Change Path Without Reloading
To change path URL with AngularJS, $location.path() is passed the new URL as a parameter, and the pa ...
- [转]SQL2005后的ROW_NUMBER()函数的应用
SQL Server 2005后之后,引入了row_number()函数,row_number()函数的分组排序功能使这种操作变得非常简单 分组取TOP数据是T-SQL中的常用查询, 如学生信息管理系 ...
- 同源策略与JSONP
[CORS:跨域资源共享] 同源策略与JSONP Web API普遍采用面向资源的REST架构,将浏览器最终执行上下文的JavaScript应用Web API消费者的重要组成部分.“同源策略”限制了J ...
- Real-Rime Rendering (1) - 渲染管线(Rendering Pipeline)
提要 渲染管线是实时渲染中最重要的部分,它的最主要的任务就是在给定一个虚拟的场景,包括相机,object,灯光,纹理等等,生成一副2D的图像. 最基础的渲染管线如下图所示: 主要的阶段包括三个:App ...
- Linux脚本学习随记
把文件件的归属转移到其他用户上chown [-R] 账号名称:用户组名称 文件或目录 在进行hadoop分布式部署的时候,需要生成密钥对具体的操作如下先在master的hadoop目录下创建.sshm ...
- cookie和session详解[转]
文章链接: http://aijezdm915.iteye.com/blog/1272530 cookie.session 都是用来保存用户状态信息的一种方法或手段 二者主要区别是: ...
- [转]Building a Basic Fuzzer with GDB: The Five Minute GDB Scripting Tutorial
link:http://www.praetorian.com/blog/building-a-basic-fuzzer-with-gdb-the-five-minute-gdb-scripting-t ...
- 使用WCF扩展记录服务调用时间
随笔- 64 文章- 0 评论- 549 真实世界:使用WCF扩展记录服务调用时间 WCF 可扩展性 WCF 提供了许多扩展点供开发人员自定义运行时行为. WCF 在 Channel Lay ...