JSP中将查询结果导出为excel




JSP中将查询结果导出为excel

转自洋葱头的BLOG   原文:http://blog.sina.com.cn/u/3f6efc4a0100063o

  如何将JSP中将查询结果导出为Excel,其实可以利用jakarta提供的POI接口将查询结果导出到excel。POI接口是jakarta组织的一个子项目,它包括POIFS,HSSF,HWSF,HPSF,HSLF,目前比较成熟的是HSSF,它是一组操作微软的excel文档的API,现在到达3.0版本,已经能够支持将图片插入到excel里面。下面简要的介绍一下它的用法,在这里http://apache.justdn.org/jakarta/poi/dev/bin/可以下载到它的最新版,下载下来以后,将它解压,并将其中的poi-3.0-alpha1-20050704.jar,poi-contrib-3.0-alpha1-20050704.jar,poi-scratchpad-3.0-alpha1-20050704.jar加入到你的开发环境的classpath中(比如JBUILDER可以点击工具》configure jdks》add……)。在解压的目录中,有一个docs目录是一些文档,其中里面有两个目录特别有用,一个是hssf目录,一个是apidocs目录。我从它的英文文档中摘出一些例子,放在一个java文件里面,加上简单的注释,你可以下载这个java文件下来玩玩。下面是java文件的一部分。

java 代码
  1. import java.io.*;   
  2. import org.apache.poi.hssf.usermodel.*;   
  3. import org.apache.poi.hssf.util.*;   
  4. import org.apache.poi.hssf.usermodel.contrib.*;   
  5. import javax.imageio.ImageIO;   
  6.   
  7. public class Main {   
  8.     public Main() {   
  9.     }   
  10.     //演示如何创建一个工作簿   
  11.     //用这种方法创建的工作簿有问题,用Excel打开以后发现下面Tab没有选中   
  12.     public static void ex1() {   
  13.         try {   
  14.             org.apache.poi.hssf.usermodel.HSSFWorkbook wb = new org.apache.poi.   
  15.                 hssf.usermodel.HSSFWorkbook();   
  16.             java.io.FileOutputStream fileOut = new java.io.FileOutputStream(   
  17.                 “ex1.xls”);   
  18.             wb.write(fileOut);   
  19.             fileOut.close();   
  20.         }   
  21.         catch (Exception eee) {   
  22.             eee.printStackTrace();   
  23.         }   
  24.     }   
  25.     //演示如何创建一个工作表   
  26.     public static void ex2() {   
  27.         try {   
  28.             org.apache.poi.hssf.usermodel.HSSFWorkbook wb = new org.apache.poi.   
  29.                 hssf.usermodel.HSSFWorkbook();   
  30.             org.apache.poi.hssf.usermodel.HSSFSheet sheet1 = wb.createSheet(   
  31.                 “new sheet”);   
  32.             org.apache.poi.hssf.usermodel.HSSFSheet sheet2 = wb.createSheet(   
  33.                 “second sheet”);   
  34.             java.io.FileOutputStream fileOut = new java.io.FileOutputStream(   
  35.                 “ex2.xls”);   
  36.             wb.write(fileOut);   
  37.             fileOut.close();   
  38.         }   
  39.         catch (Exception eee) {   
  40.             eee.printStackTrace();   
  41.         }   
  42.     }   
  43.     //演示如何创建一个单元格   
  44.     public static void ex3() {   
  45.         try {   
  46.             org.apache.poi.hssf.usermodel.HSSFWorkbook wb = new org.apache.poi.   
  47.                 hssf.usermodel.HSSFWorkbook();   
  48.             org.apache.poi.hssf.usermodel.HSSFSheet sheet = wb.createSheet(   
  49.                 “new sheet”);   
  50.             // Create a row and put some cells in it. Rows are 0 based.   
  51.             org.apache.poi.hssf.usermodel.HSSFRow row = sheet.createRow( (short)   
  52.                 0);   
  53.             // Create a cell and put a value in it.   
  54.             org.apache.poi.hssf.usermodel.HSSFCell cell = row.createCell( (short)   
  55.                 0);   
  56.             cell.setCellValue(1);   
  57.             // Or do it on one line.   
  58.             row.createCell( (short1).setCellValue(1.2);   
  59.             row.createCell( (short2).setCellValue(“This is a string”);   
  60.             row.createCell( (short3).setCellValue(true);   
  61.             // Write the output to a file   
  62.             java.io.FileOutputStream fileOut = new java.io.FileOutputStream(   
  63.                 “ex3.xls”);   
  64.             wb.write(fileOut);   
  65.             fileOut.close();   
  66.         }   
  67.         catch (Exception eee) {   
  68.             eee.printStackTrace();   
  69.         }   
  70.     }   
  71.     //演示如何操作自选图形   
  72.     public static void ex27() {   
  73.         try {   
  74.             HSSFWorkbook wb = new HSSFWorkbook();   
  75.             HSSFSheet sheet = wb.createSheet(“new sheet”);   
  76.             HSSFPatriarch patriarch = sheet.createDrawingPatriarch();   
  77.             HSSFClientAnchor a = new HSSFClientAnchor(001023255,   
  78.                 (short10, (short10);   
  79.             HSSFSimpleShape shape1 = patriarch.createSimpleShape(a);   
  80.             shape1.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);   
  81.             HSSFTextbox textbox1 = patriarch.createTextbox(   
  82.                 new HSSFClientAnchor(0000, (short11, (short22));   
  83.             textbox1.setString(new HSSFRichTextString(“This is a test”));   
  84.             HSSFFont font = wb.createFont();   
  85.             font.setItalic(true);   
  86.             font.setUnderline(HSSFFont.U_DOUBLE);   
  87.             HSSFRichTextString string = new HSSFRichTextString(“Woo!!!”);   
  88.             string.applyFont(25, font);   
  89.             textbox1.setString(string);   
  90.             // Create a shape group.   
  91.             HSSFShapeGroup group = patriarch.createGroup(   
  92.                 new HSSFClientAnchor(00900200, (short22, (short22));   
  93.             // Create a couple of lines in the group.   
  94.             shape1 = group.createShape(new HSSFChildAnchor(33500500));   
  95.             shape1.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);   
  96.             ( (HSSFChildAnchor) shape1.getAnchor()).setAnchor( (short33,   
  97.                 500500);   
  98.             HSSFSimpleShape shape2 = group.createShape(new HSSFChildAnchor( (short)   
  99.                 1200400600));   
  100.             shape2.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);   
  101.             group.setCoordinates(10102020); // top-left, bottom-right   
  102.             FileOutputStream fileOut = new FileOutputStream(“ex27.xls”);   
  103.             wb.write(fileOut);   
  104.             fileOut.close();   
  105.         }   
  106.         catch (Exception eee) {   
  107.             eee.printStackTrace();   
  108.         }   
  109.     }   
  110.     //演示如何设定outline   
  111.     public static void ex28() {   
  112.         try {   
  113.             HSSFWorkbook wb = new HSSFWorkbook();   
  114.             HSSFSheet sheet1 = wb.createSheet(“new sheet”);   
  115.             sheet1.groupRow(514);   
  116.             sheet1.groupRow(714);   
  117.             sheet1.groupRow(1619);   
  118.             sheet1.groupColumn( (short4, (short7);   
  119.             sheet1.groupColumn( (short9, (short12);   
  120.             sheet1.groupColumn( (short10, (short11);   
  121.             FileOutputStream fileOut = new FileOutputStream(“ex28.xls”);   
  122.             wb.write(fileOut);   
  123.             fileOut.close();   
  124.         }   
  125.         catch (Exception eee) {   
  126.             eee.printStackTrace();   
  127.         }   
  128.     }   
  129.     //演示如何设定outline   
  130.     public static void ex29() {   
  131.         try {   
  132.             HSSFWorkbook wb = new HSSFWorkbook();   
  133.             HSSFSheet sheet = wb.createSheet(“new sheet”);   
  134.             HSSFPatriarch patriarch = sheet.createDrawingPatriarch();   
  135.             HSSFClientAnchor a = new HSSFClientAnchor(001023255,   
  136.                 (short10, (short10);   
  137.             HSSFShapeGroup group = patriarch.createGroup(a);   
  138.             group.setCoordinates(0080 * 412 * 23);   
  139.             float verticalPointsPerPixel = a.getAnchorHeightInPoints(sheet) /   
  140.                 (float) Math.abs(group.getY2() – group.getY1());   
  141.             EscherGraphics g = new EscherGraphics(group, wb,   
  142.                                                   java.awt.Color.black,   
  143.                                                   verticalPointsPerPixel);   
  144.             EscherGraphics2d g2d = new EscherGraphics2d(g);   
  145.             //drawChemicalStructure( g2d );   
  146.             FileOutputStream fileOut = new FileOutputStream(“ex29.xls”);   
  147.             wb.write(fileOut);   
  148.             fileOut.close();   
  149.         }   
  150.         catch (Exception eee) {   
  151.             eee.printStackTrace();   
  152.         }   
  153.     }   
  154.     //演示如何处理图片   
  155.     public static void ex30() {   
  156.         try {   
  157.             HSSFWorkbook wb = new HSSFWorkbook();   
  158.             HSSFSheet sheet = wb.createSheet(“new sheet”);   
  159.             HSSFPatriarch patriarch = sheet.createDrawingPatriarch();   
  160.             HSSFClientAnchor anchor;   
  161.             anchor = new HSSFClientAnchor(000255, (short22, (short4,   
  162.                                           7);   
  163.             anchor.setAnchorType(2);   
  164.             patriarch.createPicture(anchor,   
  165.                                     loadPicture(   
  166.                 “1.jpg”,   
  167.                wb));   
  168.             FileOutputStream fileOut = new FileOutputStream(“ex30.xls”);   
  169.             wb.write(fileOut);   
  170.             fileOut.close();   
  171.         }   
  172.         catch (Exception eee) {   
  173.             eee.printStackTrace();   
  174.         }   
  175.     }   
  176.     private static int loadPicture(String filePath, HSSFWorkbook wb) {   
  177.         int result = 0;   
  178.         try {   
  179.             ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();   
  180.             java.awt.image.BufferedImage bufferImg = ImageIO.read(new File(   
  181.                 filePath));   
  182.             ImageIO.write(bufferImg,“jpg”,byteArrayOut);   
  183.             result = wb.addPicture(byteArrayOut.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG);   
  184.         }   
  185.         catch (Exception e) {   
  186.             e.printStackTrace();   
  187.         }   
  188.         return result;   
  189.     }   
  190. }  

好好学习一下这些例子,基本上就能利用java对excel进行操作了。
  那么JSP中查询页面导出到excel只要在服务器端利用HSSF形成xls文件,然后让用户来下载这些文件就行了。

发布了0 篇原创文章 ·
获赞 4 ·
访问量 1484