订阅所有JSP/Servlet的日志 订阅 | 这是最新一篇日志 上一篇 | 下一篇日志 下一篇 ]
JSP探讨

利用JAVA操作EXCEL文件(JAVA EXCEL API)

JAVA EXCEL API简介


Java Excel是一开放源码项目,通过它Java开发人员可以读取Excel文件的内容、创建新的Excel文件、更新已经存在的Excel文件。使用该API非Windows操作系统也可以通过纯Java应用来处理Excel数据表。因为是使用Java编写的,所以我们在Web应用中可以通过JSP、Servlet来调用API实现对Excel数据表的访问。

提供以下功能:

从Excel 95、97、2000等格式的文件中读取数据;
读取Excel公式(可以读取Excel 97以后的公式);
生成Excel数据表(格式为Excel 97);
支持字体、数字、日期的格式化;
支持单元格的阴影操作,以及颜色操作;
修改已经存在的数据表;
能够读取图表信息
1.应用示例:
包括从Excel读取数据,生成新的Excel,以及修改Excel
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package common.util;
 
import jxl.*;
import jxl.format.UnderlineStyle;
import jxl.write.*;
import jxl.write.Number;
import jxl.write.Boolean;
 
import java.io.*;
 
/**
 * Created by IntelliJ IDEA.
 * User: xl
 * Date: 2005-7-17
 * Time: 9:33:22
 * To change this template use File | Settings | File Templates.
 */
public class ExcelHandle
{
    public ExcelHandle()
    {
    }
 
    /**
     * 读取Excel
     *
     * @param filePath
     */
    public static void readExcel(String filePath)
    {
        try
        {
            InputStream is = new FileInputStream(filePath);
            Workbook rwb = Workbook.getWorkbook(is);
            //Sheet st = rwb.getSheet("0")这里有两种方法获取sheet表,1为名字,而为下标,从0开始
            Sheet st = rwb.getSheet("original");
            Cell c00 = st.getCell(0,0);
            //通用的获取cell值的方式,返回字符串
            String strc00 = c00.getContents();
            //获得cell具体类型值的方式
            if(c00.getType() == CellType.LABEL)
            {
                LabelCell labelc00 = (LabelCell)c00;
                strc00 = labelc00.getString();
            }
            //输出
            System.out.println(strc00);
            //关闭
            rwb.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
 
    /**
     * 输出Excel
     *
     * @param os
     */
    public static void writeExcel(OutputStream os)
    {
        try
        {
            /**
             * 只能通过API提供的工厂方法来创建Workbook,而不能使用WritableWorkbook的构造函数,
             * 因为类WritableWorkbook的构造函数为protected类型
             * method(1)直接从目标文件中读取WritableWorkbook wwb = Workbook.createWorkbook(new File(targetfile));
             * method(2)如下实例所示 将WritableWorkbook直接写入到输出流

             */
            WritableWorkbook wwb = Workbook.createWorkbook(os);
            //创建Excel工作表 指定名称和位置
            WritableSheet ws = wwb.createSheet("Test Sheet 1",0);
 
            //**************往工作表中添加数据*****************
 
            //1.添加Label对象
            Label label = new Label(0,0,"this is a label test");
            ws.addCell(label);
 
            //添加带有字型Formatting对象
            WritableFont wf = new WritableFont(WritableFont.TIMES,18,WritableFont.BOLD,true);
            WritableCellFormat wcf = new WritableCellFormat(wf);
            Label labelcf = new Label(1,0,"this is a label test",wcf);
            ws.addCell(labelcf);
 
            //添加带有字体颜色的Formatting对象
            WritableFont wfc = new WritableFont(WritableFont.ARIAL,10,WritableFont.NO_BOLD,false,
                    UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.RED);
            WritableCellFormat wcfFC = new WritableCellFormat(wfc);
            Label labelCF = new Label(1,0,"This is a Label Cell",wcfFC);
            ws.addCell(labelCF);
 
            //2.添加Number对象
            Number labelN = new Number(0,1,3.1415926);
            ws.addCell(labelN);
 
            //添加带有formatting的Number对象
            NumberFormat nf = new NumberFormat("#.##");
            WritableCellFormat wcfN = new WritableCellFormat(nf);
            Number labelNF = new jxl.write.Number(1,1,3.1415926,wcfN);
            ws.addCell(labelNF);
 
            //3.添加Boolean对象
            Boolean labelB = new jxl.write.Boolean(0,2,false);
            ws.addCell(labelB);
 
            //4.添加DateTime对象
            jxl.write.DateTime labelDT = new jxl.write.DateTime(0,3,new java.util.Date());
            ws.addCell(labelDT);
 
            //添加带有formatting的DateFormat对象
            DateFormat df = new DateFormat("dd MM yyyy hh:mm:ss");
            WritableCellFormat wcfDF = new WritableCellFormat(df);
            DateTime labelDTF = new DateTime(1,3,new java.util.Date(),wcfDF);
            ws.addCell(labelDTF);
 
 
            //添加图片对象,jxl只支持png格式图片
            File image = new File("f:\\2.png");
            WritableImage wimage = new WritableImage(0,1,2,2,image);
            ws.addImage(wimage);
            //写入工作表
            wwb.write();
            wwb.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
 
    /**
     * 拷贝后,进行修改,其中file1为被copy对象,file2为修改后创建的对象
     * 尽单元格原有的格式化修饰是不能去掉的,我们还是可以将新的单元格修饰加上去,
     * 以使单元格的内容以不同的形式表现
     * @param file1
     * @param file2
     */
    public static void modifyExcel(File file1,File file2)
    {
        try
        {
            Workbook rwb = Workbook.getWorkbook(file1);
            WritableWorkbook wwb = Workbook.createWorkbook(file2,rwb);//copy
            WritableSheet ws = wwb.getSheet(0);
            WritableCell wc = ws.getWritableCell(0,0);
            //判断单元格的类型,做出相应的转换
            if(wc.getType == CellType.LABEL)
            {
                Label label = (Label)wc;
                label.setString("The value has been modified");
            }
            wwb.write();
            wwb.close();
            rwb.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
 
 
    //测试
    public static void main(String[] args)
    {
        try
        {
            //读Excel
            ExcelHandle.readExcel("f:/testRead.xls");
            //输出Excel
            File fileWrite = new File("f:/testWrite.xls");
            fileWrite.createNewFile();
            OutputStream os = new FileOutputStream(fileWrite);
            ExcelHandle.writeExcel(os);
            //修改Excel
            ExcelHandle.modifyExcel(new file(""),new File(""));
        }
        catch(Exception e)
        {
           e.printStackTrace();
        }
    }
}

2.在jsp中做相关测试,创建一个writeExcel.jsp
1
2
3
4
5
6
7
8
<%
response.reset();//清除Buffer
response.setContentType("application/vnd.ms-excel");
File fileWrite = new File("f:/testWrite.xls");
fileWrite.createNewFile();
new FileOutputStream(fileWrite);
ExcelHandle.writeExcel(new FileOutputStream(fileWrite));
%>

在IE中浏览writeExcel.jsp就可以动态生成Excel文档了,其中response.setContentType("application/vnd.ms-excel");语句必须要,才能确保不乱码,在jsp中输入<%@page contentType="application/vnd.ms-excel;charset=GBK"%>不行。

平均得分
(0 次评分)





文章来自: ChinaitpowerJava频道
标签:
评论: 74 | 查看次数: 2005
  • 共有 74 条评论
  • 1
  • 2
  • 3
  • 4
  • 5
  • |
  • >>
游客 [2009-01-07 15:53:02]
sss


道闸-挡车器
车库门
工业门
伸缩门
卷帘门
放心包袱,奔向新生活!
免烧砖机
制砖机
加气混凝土设备
加气混凝土
加气砼设备
加气块
加气块设备
压砖机
打砖机
砖机
砖机厂

免烧砖机厂
砌块砖机
氧化锌
回转窑
制砖机
加气砼设备
加气块设备
活性石灰窑
活性石灰

陶粒砂
铝矾土
锻烧窑
金属镁
代孕
代孕
代孕
代孕
代孕

代孕

代孕
代孕
代孕
代孕
代孕


代孕
代孕
代孕
代孕
氧化锌
水泥窑
回转窑
石灰窑
烘干窑
灰沙蒸养砖设备
灰沙砖
灰沙砖设备
灰沙蒸养砖
蒸养砖
加气砖
电加热反应釜
不锈钢反应釜
血液分析仪
塔机
塔吊
塔式起重机
塔式起重机
混凝土搅拌站
烟台经济技术开发区医院
烟台开发区医院
蒸汽流量计
涡街流量计
反应釜
淀粉设备
木薯粉设备
储罐
生态鸡蛋
水性漆
水性防锈漆
水性金属漆
水性玩具漆
数控转台
过滤机
反应釜
捏合机
化工机械
不锈钢反应釜
自控系统工程
计量灌装设备
烟台西门子
不锈钢标准件
不锈钢法兰
车库门
卷帘门
花生米脱皮机
烟台船舶物资
铁屑粉碎机
排屑器
排屑机
过滤机
过滤纸
工艺纸绳
工艺纸布
无水乙醇
facom
气动工具
管道工具
硅酸钾
油水分离器
纸带过滤机
排屑机
烟台演出
烟台留学中介
钢格板
胶浆
排屑机
纸带过滤机
蒸汽流量计
气缸
液压缸
二手不绣钢反应釜
海景房
塑料鸡笼
塑料鸭笼
双T板
假山盆景
电瓶修复
反应釜
玻璃反应釜
玻璃反应釜
高压反应釜
水热合成反应釜
外贸服装批发
液压油缸
活性炭
收缩膜
注塑
变频器
软起动器
电站锅炉清洗
GGH清洗
工业清洗
铬锆铜板
结晶器铜板
铬锆铜模具
铝青铜铸件
高炉风口
高炉冷却板
铸造铜冷却壁
电炉铜水箱
电炉集束射流氧枪
电炉导电铜瓦
矿热炉铜配件
电机斜楔
结晶轮
非晶带铸辊
铬锆铜棒
铬锆铜铸件
自耗炉坩埚
大转炉氧枪喷头
电机端环
电机导条
冰淇淋粉百度
PE板
液压油缸
油炸锅
大蒜脱皮机
蚕豆切口机
脱皮机
花生脱皮机
花生烤炉

切削液集中过滤
切屑压块机
液压缸
气缸

裤业百度
洗车机已在首页 做到前五

滤油机

iron oxide red
PE再生料
美术颜料
丙烯颜料
水粉颜料
广告画颜料
直流弧焊机
气体保护焊机
等离子弧切割机


捏合机百度
导热油炉百度 google
保温材料
直线导轨
外贸服装批发料百度
塑料托盘百度
硅橡胶电热带

渔网
管缝式锚杆
piping producers
Tubing pipe11
ERW steel pipe
China casing pipes1
Steel piping manufacturers
China steel pipe fittings
China steel pipe manufacturers
China seamless steel pipes1

塑料托盘百度
硅橡胶电热带
渔网
管缝式锚杆
烟台起重安装
烟台设备搬运
烟台搬厂
洗车机
自动洗车机
N-甲酰吗啉
电刷镀
冷焊机
铸造缺陷修补机
烟台书画
塑料管帽
烟台驾校
挖掘机配件
导电膏
气动量仪
钢格板
捏合机
泡沫机械
钢丝网架板生产线
过滤机
卡盘
排屑器
动力卡盘
油气缸
游客 [2008-12-22 14:04:12]
游客 [2008-12-12 11:22:31]
游客 [2008-12-08 15:50:45]
游客 [2008-12-05 13:29:52]
游客 [2008-12-03 10:47:17]
游客 [2008-12-02 15:43:57]
游客 [2008-11-27 15:07:48]
游客 [2008-11-13 13:14:49]
游客 [2008-11-12 14:44:40]
游客 [2008-11-09 15:37:01]
游客 [2008-11-07 15:10:50]
游客 [2008-11-05 15:35:22]
cheap wow power leveling Beyond knowing your limitations. HDRO gold it's important to be able to size up opponents lecteur mp3 at a glance. level wow Knowing their class is the first step, understanding. lord of rings online gold your places in a rock-paper-scissors scenario. lotro gold More important, however, is the estimation .lotro gold of gear. mp3 As much as skill and class
balance . mp3 mp4 player plays a part in the outcome . mp3 mp4 player of combat, gear is a major differentiator that makes up for shortcomings in other areas. mp3 mp4 player In fact, with . mp3 player kaufen the introduction of Resilience, gear more than mp4 ever plays a more substantial part in PvP. power level In an Arena match, the very first thing we . power level scope out is gear. wow level Through quick tab-selection viewing .wow leveling of character portraits, we generally wow lvl have a good idea of the classes we're up against if they keep their helm graphic on. wow lvl If they are wearing Season 3 shoulders, then we know . wow lvl 60 their relative experience. wow lvl 70 This is why the visual i.wow power leveling mpact of Arena shoulders is so important.wow powerlevel It immediately gives you a general idea of how tough the match will be. wow powerlevel Players in full S3 will likely have over 10k hp and over. wow powerleveling 400 Resilience, depending on the class and spec. A full S3 SL/SL Warlock, for example, will easily have. about 12-13k hp and over 400 Resilience. Identifying weapons is slightly more . difficult but will also give a general idea of an enemy's strength. Season 1 and 2 weapons share the same graphics, so it's harder to identify. Season 3 weapons, on the other hand, are distinctive and share models with Black Temple and Mount Hyjal weapons. which 最新免费网络游戏 have relatively the same power. A review of Brutal Gladiator weapons will come in handy because these will be the most common way to identify opponents of relative skill. With the new mechanics. in place for Arenas, Season 4 will more or less weed out the chaff from the grain.
游客 [2008-11-05 15:34:55]
4GB MP3 PLAYER My wife is a shrewd little fox. Bluetooth Headset She knows just how much I love the fact. Bluetooth Headsets that she plays the game with me so sometimes, when we have our little domestic arguments, she makes sure. cell phone accessories to cancel her WoW account just to drive home a point. des po wow Of course, it doesn't mean much since we're both paid up for the next few months, but the message is clear -- "we make up (or you see things my way). digital camcorder or I'm quitting the game!" Of course, we don't reconcile merely because I'll be losing my . digital camcorders favorite playing partner, but I have to confess that it doesn't make . dvd players me happy one bit. free online games For parents, World of Warcraft can be a . free online war games useful bargaining chip for their kids with the parental . gold wow controls feature. gold wow It's easy enough to control WoW time if kids aren't doing their homework, floundering in school, or simply not. mp3 mp4 player doing their chores. mp3 player Conversely, a friend . mp3 player accessories of mine gave his son a WoW subscription when . mp3 player accessory he did well in school. mp3 player kaufen World of Warcraft can be so. online games much fun and addicting that it's . play war games often used as a social tool, and it's often upsetting when our. po wow friends quit playing the game. portable dvd player How many of us have . wow europe had friends whose significant others have "allowed" them to. wow geld play the game after, say, a wonderful date. wow gold verkaufen I'm not sure if it only applies to me, but . wow level service because I play the game with many of my RL friends . wow leveling service and my family, I use the lure of WoW . to full effect. I once had my brother do a specific task for . the promise of an upgrade to The Burning Crusade. A little before he finished what I asked him to do, I secretly upgraded his account. so he could免费网络游戏 finally make his Blood Elf Priest. Kind of manipulative, I know, but we did end up having . a lot of fun leveling our alts together. How about you. How much a part of your life is WoW and has it .最新网游 ever been used as a bargaining . chip in your social life.
游客 [2008-11-05 15:32:23]
mp3 8GB MP3 PLAYER A couple of people . apple ipod have posted about the Shattered Sun Peacekeepers slacking off on their jobs lately. buy cheap wow gold quite possibly thanks . canon digital camera to something Blizzard fed them . cheap world of warcraft gold in PatchIn their reports, they complain about Peacekeepers. digital camera attacking them for no reason, sometimes not even in retaliation for attacking (or defending yourself against). digital cameras a member of the opposing faction. dvd player I can actually empathize with this as. eve isk I encountered the ill-placed wrath of the Peacekeepers myself when I rezzed my wife's toon in front of . ipod the Staging Area. ipod nano Without having done anything other. ipod shuffle than rezz, the Peacekeepers promptly charged . ipod touch and made short work of me. ipods In my experience, I have found 网游 that the Peacekeepers around . mp3 the Shattered Sun Staging Area have . mp3 player been slacking off. mp3 players In fact, my wife's toon was ganked right in front . mp4 of the building and the so-called Peacekeepers . portable dvd players did absolutely nothing. world of warcraft buy gold Sensing a bug, my wife . wow wrote a ticket and got a somewhat rude e-mail response saying that -- you guessed it -- it was working as intended. wow gold An Alliance . wow gold guild on my server seemed to be aware of the fact and exploited it to full effect, killing solo players who could seek no refuge under the apathetic -- or even hostile -- Peacekeepers. wow leveling According to reports, it's a known bug -- one player even . wow powerleveling made a video to document it -- but so far Blizzard . zubehoer mp3 player has turned a blind eye to it. I got a more sympathetic response 网络游戏 from my GM who at least. mentioned he'd look into the situation. An in-game GM even reset the Peacekeepers in order to see if it would change anything (it didn't). I don't mind gankage
  • 共有 74 条评论
  • 1
  • 2
  • 3
  • 4
  • 5
  • |
  • >>
发表评论
昵 称:  登录
内 容:
选 项:
字数限制 1000 字 | UBB代码 开启 | [img]标签 开启