POI是一个可以对excel文件进行操作的jar包,使用它可以帮助我们对excel进行操作,也就可以帮助我们实现在jsp页面添加导入数据的功能。只要我们在控制层servlet中加入处理的方法就可以了;首先使用到POI都会与JXL进行对比:查阅之后大致是这样的

一、jxl优点:Jxl对中文支持非常好,操作简单,方法看名知意。

Jxl是纯javaAPI,在跨平台上表现的非常完美。 支持字体、数字、日期操作,能够修饰单元格属性, 支持图像和图表,但是这套API对图形和图表的支持很有限,而且仅仅识别PNG格式。缺点:效率低,图片支持不完善,对格式的支持不如POI强大

二、POI优点:效率高。支持公式,宏,一些企业应用上会非常实用 ,能够修饰单元格属性。 支持字体、数字、日期操作。缺点:不成熟,代码不能跨平台。

是具体使用那个包,看你使用的具体情况来定。下面是POI工具类的使用

POI工具类为:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.util.CellRangeAddress;
/*OutputStream out = response.getOutputStream();//response.reset();response.setContentType("application/vnd.ms-excel");response.setHeader("Content-disposition", "attachment; fileName=" + new String(("duty.xls").getBytes(), "iso8859-1"));// OutputStream outputStream = new FileOutputStream("D:/students.xls");workbook.write(out);out.close();*/
public class ExcelOperate {public static void main(String[] args) {// 创建Excel表格createExcel(getStudent());// 读取Excel表格// List<Student> list = readExcel();// System.out.println(list.toString());}/*** 初始化数据* * @return 数据*/private static List<Student> getStudent() {List<Student> list = new ArrayList<Student>();Student student1 = new Student("小明", 8, "二年级");Student student2 = new Student("小光", 9, "三年级");Student student3 = new Student("小花", 10, "四年级");list.add(student1);list.add(student2);list.add(student3);return list;}/*** 创建Excel* * @param list*            数据*/private static void createExcel(List<Student> list) {// 创建一个Excel文件HSSFWorkbook workbook = new HSSFWorkbook();// 创建一个工作表HSSFSheet sheet = workbook.createSheet("学生表一");CellRangeAddress region = new CellRangeAddress(0, // first row0, // last row0, // first column2 // last column);sheet.addMergedRegion(region);HSSFRow hssfRow = sheet.createRow(0);HSSFCell headCell = hssfRow.createCell(0);headCell.setCellValue("学生列表");// 设置单元格格式居中HSSFCellStyle cellStyle = workbook.createCellStyle();cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);/*cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);// cellStyle.setFillBackgroundColor(HSSFColor.GREEN.index);cellStyle.setFillForegroundColor(HSSFColor.GREEN.index);HSSFFont font = workbook.createFont();font.setFontName("楷体"); //字体font.setFontHeightInPoints((short)30); //字体大小font.setColor(HSSFColor.RED.index);//颜色font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//加粗font.setItalic(true); //倾斜cellStyle.setFont(font);*/headCell.setCellStyle(cellStyle);// 添加表头行hssfRow = sheet.createRow(1);// 添加表头内容headCell = hssfRow.createCell(0);headCell.setCellValue("姓名");headCell.setCellStyle(cellStyle);headCell = hssfRow.createCell(1);headCell.setCellValue("年龄");headCell.setCellStyle(cellStyle);headCell = hssfRow.createCell(2);headCell.setCellValue("年级");headCell.setCellStyle(cellStyle);// 添加数据内容for (int i = 0; i < list.size(); i++) {hssfRow = sheet.createRow((int) i + 2);Student student = list.get(i);// 创建单元格,并设置值HSSFCell cell = hssfRow.createCell(0);cell.setCellValue(student.getName());cell.setCellStyle(cellStyle);cell = hssfRow.createCell(1);cell.setCellValue(student.getAge());cell.setCellStyle(cellStyle);cell = hssfRow.createCell(2);cell.setCellValue(student.getGrade());cell.setCellStyle(cellStyle);}// 保存Excel文件try {OutputStream outputStream = new FileOutputStream("D:/students.xls");workbook.write(outputStream);outputStream.close();} catch (Exception e) {e.printStackTrace();}}/*** 读取Excel* * @return 数据集合*/private static List<Student> readExcel() {List<Student> list = new ArrayList<Student>();HSSFWorkbook workbook = null;try {// 读取Excel文件InputStream inputStream = new FileInputStream("D:/students.xls");workbook = new HSSFWorkbook(inputStream);inputStream.close();} catch (Exception e) {e.printStackTrace();}// 循环工作表for (int numSheet = 0; numSheet < workbook.getNumberOfSheets(); numSheet++) {HSSFSheet hssfSheet = workbook.getSheetAt(numSheet);if (hssfSheet == null) {continue;}// 循环行for (int rowNum = 2; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {HSSFRow hssfRow = hssfSheet.getRow(rowNum);if (hssfRow == null) {continue;}// 将单元格中的内容存入集合Student student = new Student();HSSFCell cell = hssfRow.getCell(0);if (cell == null) {continue;}student.setName(cell.getStringCellValue());cell = hssfRow.getCell(1);if (cell == null) {continue;}student.setAge((int) cell.getNumericCellValue());cell = hssfRow.getCell(2);if (cell == null) {continue;}student.setGrade(cell.getStringCellValue());list.add(student);}}return list;}
}class Student {private String name;private int age;private String grade;public Student() {}public Student(String name, int age, String grade) {super();this.name = name;this.age = age;this.grade = grade;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getGrade() {return grade;}public void setGrade(String grade) {this.grade = grade;}@Overridepublic String toString() {return "Student [name=" + name + ", age=" + age + ", grade=" + grade+ "]";}
}

这个里面是具体的用法,与对一个学生列表实现的案例;使用该工具我们只要在servelt中调用工具类的创建excel与实现excel的方法即可;就像:

                DutyService ds=new DutyServiceImpl();List<Duty> dutyList=ds.findBy(empId,deptno,dtDate);//返回输出流createExcel(dutyList,resp);

我将查询返回到的列表传输到创建excel的方法,并且对excel的方法进行了重写,在与前台页面进行交互的时候,因为要对客户端进行写入操作,而不是对服务器端进行操作,所以我将response也传入进去,因为我们不能简单对返回的数据进行重定向或者请求转发操作,而是以流的方式写回去

  try {resp.setContentType("application/vnd.ms-excel");//声明导出excelresp.setHeader("Content-disposition", "attachment;filename=duty.xls");//附件形式下载包括名字//OutputStream outputStream = new FileOutputStream("D:/kaoqin.xls");ServletOutputStream outputStream = resp.getOutputStream();workbook.write(outputStream);outputStream.close();} catch (Exception e) {e.printStackTrace();}

poi 设置word表格颜色_POI工具练习相关推荐

  1. poi 设置word表格颜色_办公软件小课堂 Word表格的设置

    你是否还在为考证烦恼?是否不知从何学起?学习部每周的办公软件小课堂!这里总有你想要的! 本周给大家整理的是 Word表格的设置 01 插入选择卡→表格 02 文本转表格 步骤1:选中要转换成表格的文本 ...

  2. poi设置word表格单元格宽度_poi导出word表格的操作讲解

    一.效果如下 二.js代码 function export_word(){ //导出word var url = "czzsca/exportWord.do"; this.expo ...

  3. poi设置word表格单元格宽度_poi,word,表格样式

    第 1 页 共 17 页 竭诚为您提供优质文档 / 双击可除 poi,word, 表格样式 篇一: poi 操作 wrod 技巧 1. 表格或单元格宽度: 默认 tblw 的 type 属性为 stt ...

  4. poi设置word表格单元格宽度_xwpftable设置宽度;POI操作Word设置表格宽度

    poi 操作word里表格,如设置表格宽度.行高.表格样式等. 1.表格或单元格宽度: 默认TblW的type属性为STTblWidth.AUTO,即自动伸缩.所以要调整为指定类型:STTblWidt ...

  5. poi设置word表格单元格宽度_java poi如何设置word的页面的大小和水平方向?

    展开全部 你好,试试以下代码行不行. package com.sample; import java.awt.color; import java.io.fileoutputstream; impor ...

  6. poi设置word表格单元格宽度_java poi 设置word 格式如表格一类的

    2012-05-29 回答 你好,试试以下代码行不行. package com.sample; import java.awt.color; import java.io.fileoutputstre ...

  7. poi设置word表格单元格宽度_java poi 生成word表格 怎么逐列设置列宽

    九州编程 public static int[] COLUMN_WIDTHS = new int[] {1504,1504,1504,1504,1504,1504}; setTableGridCol( ...

  8. POI设置word表格的行在各页顶端以标题形式重复出现

    CTRow ctRow = row.getCtRow(); CTTrPr trPr = ctRow.isSetTrPr() ? ctRow.getTrPr() : ctRow.addNewTrPr() ...

  9. Java使用Poi填充Word表格模板(图片和文字)

    Java使用Poi填充Word表格模板(图片和文字) **** 由于个人需求需要对表格模板进行操作,所以本文章只对表格进行替换数据操作,没有段落,没有循环遍历,没有延伸!!!!!(后续补充!!!) * ...

最新文章

  1. 计算机专业活动简报,计算机系辩论赛活动简报
  2. 有哪些命令行工具堪称神器?
  3. Redis集群~StackExchange.Redis(10月6号版1.1.608.0)连接Twemproxy支持Auth指令了
  4. 真机x86 android分辨率,Android-x86入门之--启动参数设置
  5. 设计模式之 里氏替换原则
  6. centos 6.5 防火墙开放指定端口
  7. vc6可编译的 linux 源码,winpcap+vc6编译环境配置,以及获取网络设备列表的源代码...
  8. 语音识别——触发字检测
  9. java编程给三个数字排序_JAVA程序.输入3个数字,有IF语句,从小到大排序
  10. Shell脚本--并发执行
  11. 在html中怎样使用%3c符号,5个JavaScript和9个JQuery经典面试题
  12. Web前端基础学习——HTML5基础
  13. 高尔顿钉板实验的matlab代码动画演示
  14. 计算机应用选购哪些电脑,购买电脑有哪些要注意的
  15. 用计算机制作母亲贺卡,母亲节电子贺卡制作
  16. wirehark数据分析与取证attack.pcap
  17. 端午节,我用Python爬取屈原的诗
  18. APP端测试常见的功能点
  19. 辛辛苦苦学C语言究竟有什么用?
  20. [Codeforces 940E]Cashback

热门文章

  1. java接口如何接受语音参数_Java 是如何优雅地实现接口数据校验的?
  2. jeecg自定义结果集t:dictSelect
  3. MySQL中OR和AND的区别是什么____MySQL中or与in
  4. Springboot中@ComponentScan 注解
  5. RabbitMQ保姆级教程
  6. SSM中PageHelper的使用步骤与com.github.pagehelper.PageHelper3系列与5系列的区别
  7. 8客户端安装后无法启动_新君越涉水后车辆无法启动
  8. HBase 的表设计
  9. url中隐藏php后缀,url中如何隐藏.php
  10. Android日志[进阶篇]一-使用 Logcat 写入和查看日志