文章结尾源码以及jxl包

1、输出excel:

package JmExcel;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import jxl.Workbook;
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; public class JavaMakeExcel {
private List list = new ArrayList(); public void createQuarterlyExcel(OutputStream os) throws IOException, RowsExceededException, WriteException, InterruptedException{
//创建一个工作薄excel文档
WritableWorkbook workbook = Workbook.createWorkbook(os);
//创建一个表sheet,一个工作薄可以有多个sheet
WritableSheet sheet = workbook.createSheet("季度报", 0);
//设置列宽,行列开始下标均为0
sheet.setColumnView(0, 4);
sheet.setColumnView(1, 10);
sheet.setColumnView(2, 16);
sheet.setColumnView(3, 20);
sheet.setColumnView(4, 8);
sheet.setColumnView(5, 12);
sheet.setColumnView(6, 7);
sheet.setColumnView(7, 12);
sheet.setColumnView(8, 20);
sheet.setColumnView(9, 20);
sheet.setColumnView(10,20);
sheet.setColumnView(11,16); Calendar cal = Calendar.getInstance();
//添加合并单元格,第一个参数是起始列,第二个参数是起始行,第三个参数是终止列,第四个参数是终止行
sheet.mergeCells(0, 0, 1, 0);
//创建一个单元格,第一个参数为开始列,第二个参数为开始行,第三个参数为填充内容,第四个参数(可省略)内容格式
Label fj = new Label(0,0,"附件:");
//在表中添加该单元格
sheet.addCell(fj);
//合并单元格,第一个参数是起始列,第二个参数是起始行,第三个参数是终止列,第四个参数是终止行
sheet.mergeCells(0, 1, 11, 1);
//创建内容样式1:表头样式,设置字体种类和黑体显示,字体为Arial,字号大小为22,加粗
WritableFont bold = new WritableFont(WritableFont.createFont("宋体"),22,WritableFont.BOLD);
WritableCellFormat titleFormate = new WritableCellFormat(bold);//生成一个单元格样式控制对象
titleFormate.setAlignment(jxl.format.Alignment.CENTRE);//单元格中的内容水平方向居中
titleFormate.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);//单元格的内容垂直方向居中
//Label(开始列,开始行,labeltitle,格式化);
Label title = new Label(0,1,"**区"+cal.get(cal.YEAR)+"年市委市政府重点工作落实情况表",titleFormate);
//设置行高,.setRowView(行下标,高度值)
sheet.setRowView(1, 700, false);
sheet.addCell(title); //创建内容样式2:表列头样式
WritableFont color = new WritableFont(WritableFont.createFont("宋体"),10,WritableFont.BOLD);
WritableCellFormat colorFormat = new WritableCellFormat(color);
colorFormat.setAlignment(jxl.format.Alignment.CENTRE);
colorFormat.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
colorFormat.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
colorFormat.setWrap(true);
//创建内容样式3
WritableFont size = new WritableFont(WritableFont.createFont("宋体"),8,WritableFont.BOLD);
WritableCellFormat colorFormat2 = new WritableCellFormat(size);
colorFormat2.setAlignment(jxl.format.Alignment.CENTRE);
colorFormat2.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
colorFormat2.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
colorFormat2.setWrap(true); sheet.setRowView(2, 350, false);
//填充表头
sheet.mergeCells(0, 2, 0, 3);
Label index = new Label(0,2,"序号",colorFormat);
sheet.addCell(index);
sheet.mergeCells(1, 2, 1, 3);
Label leibie = new Label(1,2,"类别",colorFormat);
sheet.addCell(leibie);
sheet.mergeCells(2, 2, 2, 3);
Label aim = new Label(2,2,"区年度具体目标",colorFormat);
sheet.addCell(aim);
Label aim2 = new Label(3,2,"工作目标",colorFormat);
sheet.addCell(aim2);
Label quarter = new Label(3,3,"三季度",colorFormat);
sheet.addCell(quarter);
sheet.mergeCells(4, 2, 4, 3);
Label localHead = new Label(4,2,"区分管领导",colorFormat);
sheet.addCell(localHead);
sheet.mergeCells(5, 2, 5, 3);
Label deptname = new Label(5,2,"牵头单位",colorFormat);
sheet.addCell(deptname);
sheet.mergeCells(6, 2, 6, 3);
Label checker = new Label(6,2,"责任人",colorFormat);
sheet.addCell(checker);
sheet.mergeCells(7, 2, 7, 3);
Label assist = new Label(7,2,"协办单位",colorFormat);
sheet.addCell(assist);
sheet.mergeCells(8, 2, 8, 3);
Label finish = new Label(8,2,"落实情况",colorFormat);
sheet.addCell(finish);
sheet.mergeCells(9, 2, 9, 3);
Label problem = new Label(9,2,"存在问题",colorFormat);
sheet.addCell(problem);
sheet.mergeCells(10, 2, 10, 3);
Label next = new Label(10,2,"下部措施",colorFormat);
sheet.addCell(next);
sheet.mergeCells(11, 2, 11, 3);
Label waring = new Label(11,2,"完成状态(按计划进行、进度顺利但有延误风险、延误)",colorFormat2);
sheet.addCell(waring);
//创建内容样式4:表正文样式
WritableFont zw = new WritableFont(WritableFont.createFont("宋体"),10);
WritableCellFormat zwFormat = new WritableCellFormat(zw);
zwFormat.setAlignment(jxl.format.Alignment.CENTRE);
zwFormat.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
zwFormat.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
zwFormat.setWrap(true); //换行:正文样式添加自动换行
//遍历输出正文
for (int i = 0; i < list.size(); i++) {
Map m = (Map) list.get(i);
String lbstr = String.valueOf(m.get("lb"));
String year_target = String.valueOf(m.get("year_target"));
String quarterly_target = String.valueOf(m.get("quarterly_target"));
String head_name = String.valueOf(m.get("head_name"));
String person_liable = String.valueOf(m.get("person_liable"));
String main_dept_name = String.valueOf(m.get("main_dept_name"));
String assist_dept_name = String.valueOf(m.get("assist_dept_name"));
String contentes = String.valueOf(m.get("contentes"));
String problem1 = String.valueOf(m.get("problem"));
String nextplan = String.valueOf(m.get("nextplan"));
String waringstr = null;
String struts = String.valueOf(m.get("status"));
struts= (struts.equals("null")) ? "1" : struts;
int waringflag = Integer.parseInt(struts) ;
if (waringflag == 1) {
waringstr = "按计划进行";
}else if (waringflag == 2) {
waringstr = "进度顺利但有延误风险";
}else if (waringflag == 3) {
waringstr = "延误";
} Label index2 = new Label(0,i+4,String.valueOf(i+1),zwFormat);
sheet.addCell(index2);
Label lb = new Label(1,i+4,lbstr,zwFormat);
sheet.addCell(lb);
Label year_targetl = new Label(2,i+4,year_target,zwFormat);
sheet.addCell(year_targetl);
Label quarterly_targetl = new Label(3,i+4,quarterly_target,zwFormat);
sheet.addCell(quarterly_targetl);
Label head_namel = new Label(4,i+4,head_name,zwFormat);
sheet.addCell(head_namel);
Label main_dept_namel = new Label(5,i+4,main_dept_name,zwFormat);
sheet.addCell(main_dept_namel);
Label person_liablel = new Label(6,i+4,person_liable,zwFormat);
sheet.addCell(person_liablel);
Label assist_dept_namel = new Label(7,i+4,assist_dept_name,zwFormat);
sheet.addCell(assist_dept_namel);
Label contentesl = new Label(8,i+4,contentes,zwFormat);
sheet.addCell(contentesl);
Label probleml = new Label(9,i+4,problem1,zwFormat);
sheet.addCell(probleml);
Label nextplanl = new Label(10,i+4,nextplan,zwFormat);
sheet.addCell(nextplanl);
Label waringstrl = new Label(11,i+4,waringstr,zwFormat);
sheet.addCell(waringstrl);
} workbook.write();
workbook.close();
os.close();
} public static void main(String[] args) throws RowsExceededException, WriteException, IOException {
JavaMakeExcel jmXL = new JavaMakeExcel();
Map map = new HashMap();
map.put("lb", "市重点工作");
map.put("year_target", "深入落实贯彻党的十九大会议精神");
map.put("quarterly_target", "新开工和筹建9660套");
map.put("head_name", "ckx");
map.put("person_liable", "戴敏");
map.put("main_dept_name", "区纪委");
map.put("assist_dept_name", "督查室");
map.put("contentes", "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111");
map.put("problem", "1111111111111111111111111111111111111111111111111111111111111111111");
map.put("nextplan", "1111111");
map.put("status", "1"); Map map2 = new HashMap();
map2.put("lb", "区常委会议定");
map2.put("year_target", "刨路修路挣外快");
map2.put("quarterly_target", "再刨一百公里");
map2.put("head_name", "zrt");
map2.put("person_liable", "任公仆");
map2.put("main_dept_name", "住建局");
map2.put("assist_dept_name", "城管大队");
map2.put("contentes", "222222212222222222222222222222222222222222222222222222222222");
map2.put("problem", "2222222");
map2.put("nextplan", "2222222");
map2.put("status", "2");
//List list = new ArrayList();
jmXL.list.add(map);
jmXL.list.add(map2);
//创建文件保存地址
File file=new File("D:/myeclipseworkpase/我的Java输出Excel.xls");
//新建文件输出流
OutputStream os = new FileOutputStream(file);
try {
jmXL.createQuarterlyExcel(os);
} catch (InterruptedException e) {
System.out.println("输出失败!");
}
} }

2、读取:

package JmExcel;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException; public class JavaReadExcel{ private String filePath;
private List list = new ArrayList(); public JavaReadExcel(String filePath){
this.filePath = filePath;
} private List readExcel() throws IOException, BiffException{
//创建输入流
InputStream stream = new FileInputStream(filePath);
//获取Excel文件对象
Workbook rwb = Workbook.getWorkbook(stream);
//获取文件的指定工作表 默认的第一个
Sheet sheet = rwb.getSheet(0);
//行数(表头的目录不需要,从1开始) sheet.getRows()获取excel行数
for(int i=0; i<sheet.getRows(); i++){
//创建一个数组 用来存储每一列的值 sheet.getColumns()获取excel列数
String[] str = new String[sheet.getColumns()];
Cell cell = null;
//列数
for(int j=0; j<sheet.getColumns(); j++){
//获取第i行,第j列的值 sheet.getCell(逐行,逐列)
cell = sheet.getCell(j,i);
str[j] = cell.getContents();
}
//把刚获取的列存入list
list.add(str);
}
return list;
} private void outData(){
for(int i=0;i<list.size();i++){
String[] str = (String[])list.get(i);
for(int j=0;j<str.length;j++){
System.out.print(str[j]+'\t');
}
System.out.println();
}
} public static void main(String args[]) throws BiffException, IOException{
JavaReadExcel excel = new JavaReadExcel("D:/myeclipseworkpase/我的Java输出Excel.xls");
List li = excel.readExcel();
for(int i=0;i<li.size();i++){
String[] str = (String[])li.get(i);
for(int j=0;j<str.length;j++){
System.out.print(str[j]+'\t');
}
System.out.println();
}
}
}

源码:

链接:http://pan.baidu.com/s/1kVL2DWr 密码:h643

java 运用jxl 读取和输出Excel的更多相关文章

  1. Java使用POI读取和写入Excel指南

    Java使用POI读取和写入Excel指南 做项目时经常有通过程序读取Excel数据,或是创建新的Excel并写入数据的需求: 网上很多经验教程里使用的POI版本都比较老了,一些API在新版里已经废弃 ...

  2. Java使用POI读取和写入Excel指南(转)

    做项目时经常有通过程序读取Excel数据,或是创建新的Excel并写入数据的需求: 网上很多经验教程里使用的POI版本都比较老了,一些API在新版里已经废弃,这里基于最新的Apache POI 4.0 ...

  3. Java使用jxl.jar包写Excel文件的最适合列宽问题基本实现

    以前用jxl.jar包,读写过Excel文件.也没有注意最适合列宽的问题,但是jxl.jar没有提供最适合列宽的功能,上次用到写了一下,可以基本实现最适合列宽. 注意,这个只是基本可以实现,基本针对中 ...

  4. Java 添加、读取、删除Excel图片

    本文介绍在Java程序中如何添加图片到excel表格,添加图片时可设置图片大小.位置.旋转.超链接.可选文本等,以及如何读取.删除excel表格中已有的图片. 工具:Free Spire.XLS fo ...

  5. Java 添加、读取、删除Excel文本框

    本文介绍通过Java程序添加文本框到Excel的方法,添加文本框时,可以添加文本.设置文本方向.文本对齐方式.设置文本框大小.位置.填充色/填充图片.文本框旋转角度.文本框名称.可选文本.文本框隐藏或 ...

  6. Java通过jxl读取Excel

    package com.hd.all.test.testjava; import java.io.File; import java.io.IOException; import java.util. ...

  7. JAVA利用jxl读取Excel内容

    JAVA可以利用jxl简单快速的读取文件的内容,但是由于版本限制,只能读取97-03  xls格式的Excel. import java.io.File; import java.io.FileInp ...

  8. Java 用jxl读取excel并保存到数据库(此方法存在局限,仅限本地电脑操作,放在服务器上的项目,需要把文件上传到服务器,详细信息,见我的别的博客)

    项目中涉及到读取excel中的数据,保存到数据库中,用jxl做起来比较简单. 基本的思路: 把excel放到固定盘里,然后前段页面选择文件,把文件的名字传到后台,再利用jxl进行数据读取,把读取到的数 ...

  9. JXL 读取 Excel java中jxl导出数据到excel的例子 上传文件

    2010-10-14 19:17:06 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info 信息: Entferne Dat ...

  10. Java 添加、读取、删除Excel形状

    本文介绍通过java程序在excel中操作形状(图形)的方法,包括: 1. 添加形状(如设置形状类型/位置/大小.形状颜色填充(单色/渐变色/纹理/图片填充).形状显示或隐藏.形状倾斜角度.添加文本到 ...

随机推荐

  1. 采云链SRM SaaS供应商管理系统,发展型中小企业的福音

    采购业务的发展遵循一些规律:采购从一开始围绕"供应商"开展,逐渐发展成围绕"货物"进行,如今围绕"供应商协同"和"采购流程管理&q ...

  2. java 验证手机号是否合法

    一.通用工具类编写 /** * @project * @Description * @Author songwp * @Date 2022/9/15 17:06 * @Version 1.0.0 ** ...

  3. windows下 Rust 环境配置

    搭建 Visual Studio Code 开发环境 首先,需要安装最新版的 Rust 编译工具和 Visual Studio Code. Rust 编译工具:https://www.rust-lan ...

  4. Kubeadm搭建高可用(k8s)Kubernetes v1.24.0集群

    文章转载自:https://i4t.com/5451.html 背景 Kubernetes 1.24新特性 从kubelet中移除dockershim,自1.20版本被弃用之后,dockershim组 ...

  5. Linux服务器上MinIO生产部署的内核调优

    #!/bin/bash cat > sysctl.conf <<EOF # maximum number of open files/file descriptors fs.file ...

  6. 回滚Deployment控制器下的应⽤发布

    若因各种原因导致滚动更新⽆法正常进⾏,如镜像⽂件获取失败."⾦丝雀"遇险等,则应该将应⽤回滚到之前的版本,或者回滚到由⽤户指定的历史记录中的版本. Deployment控制器的回滚 ...

  7. 天天向上力量B

    N=eval(input()) up=pow(1+0.001*N,365) down=pow(1-0.001*N,365) print("{:.2f}, {:.2f}, {:.0f}&quo ...

  8. [题解] Codeforces 1548 C The Three Little Pigs 组合数学,生成函数

    题目 首先令\(x=i\)时的答案为\(f_i\) ,令\(f_i\)对应的普通生成函数为\(F(x)\). 很容易发现\(F(x)=\sum_{i=0}^n (1+x)^{3i}\),sigma是在 ...

  9. 洛谷P1714 切蛋糕(单调队列)

    先放代码...... 1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N=5e5+10,M=0x3f3f3f3f; ...

  10. HDU3949/AcWing210 XOR (高斯消元求线性基)

    求第k小的异或和,用高斯消元求更简单一些. 1 //用高斯消元求线性基 2 #include<bits/stdc++.h> 3 using namespace std; 4 #define ...