SSH框架 POI例子小结

来源:互联网 发布:西安哪里可以学php 编辑:程序博客网 时间:2024/06/05 11:20

关于POI中创建excel表的各种小例子

通过建立excel表格最后输出到本地H:盘中

自己理解的:

1.首先建立工作簿——就是相当于建立一个文件

HSSFWorkbook wb = new HSSFWorkbook();

2.在工作簿上建立表空间,就是excel表格下面的sheet1,sheet2,sheet3

HSSFSheet sheet = wb.createSheet("first sheet");wb.createSheet("second sheet");

3.在表空间里面加第一行

HSSFRow row = sheet.createRow(0);

4.在第一行里面加入第一个cell单元格

HSSFCell cell = row.createCell(0);//第一行

5.对这个cell单元格进行各种style和value值的改变

cell.setCellValue(false);//第一行第一列写入value

6.最后通过outputStream流输出

wb.write(new FileOutputStream(new File("H:/poi.xls")));


难过不太会使csdn的东西







代码如下:

package com.emsp.website.safetyStudy.test;

import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.util.Calendar;import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFCellStyle;import org.apache.poi.hssf.usermodel.HSSFDataFormat;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;public class PoiTest {/** * @param args * @throws IOException  * @throws FileNotFoundException  */public static void main(String[] args) throws FileNotFoundException, IOException {//工作薄HSSFWorkbook wb = new HSSFWorkbook();//工作表HSSFSheet sheet = wb.createSheet("first sheet");wb.createSheet("second sheet");//行HSSFRow row = sheet.createRow(0);//单元格HSSFCell cell = row.createCell(0);//第一行cell.setCellValue(false);//第一行第一列row.createCell(1).setCellValue(Calendar.getInstance());//第一行第二列:可以设置(boolean|Calendar|date|String|double|RichTextString)row.createCell(2).setCellValue(12345433.532343);//double型row.createCell(3).setCellValue("ddddddddddddddddddddddddddddddddddddddd");//String 型//创建数据格式对象HSSFDataFormat format = wb.createDataFormat();//格式化,设置单元格的样式HSSFCellStyle style = wb.createCellStyle();style.setDataFormat(format.getFormat("yyyy-HH-dd hh:mm:ss"));row.getCell(1).setCellStyle(style);//给第一行第二列添加正确的样式//double值格式化style = wb.createCellStyle();style.setDataFormat(format.getFormat("#,###.000"));//科学计数法运用row.getCell(2).setCellStyle(style);//设置列宽(单位:1/20点)sheet.setColumnWidth(1, 6000);sheet.autoSizeColumn(2);//自动扩宽第三列sheet.setColumnWidth(3, 7000);//自动回绕文本style = wb.createCellStyle();style.setWrapText(true);row.getCell(3).setCellStyle(style);/** * 设置文本对齐方式 */row = sheet.createRow(1);row.createCell(0).setCellValue("左上");row.createCell(1).setCellValue("中中");row.createCell(2).setCellValue("右下");//设置行高row.setHeightInPoints(50);sheet.setColumnWidth(0, 5000);//设置列高sheet.setColumnWidth(1, 5000);sheet.setColumnWidth(2, 5000);//设置文本对齐方式--左上style = wb.createCellStyle();style.setAlignment(HSSFCellStyle.ALIGN_LEFT);style.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);row.getCell(0).setCellStyle(style);//左上//设置文本对齐方式--中中style = wb.createCellStyle();style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//设置中style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//设置中row.getCell(1).setCellStyle(style);//左上//设置文本对齐方式--右下style = wb.createCellStyle();style.setAlignment(HSSFCellStyle.ALIGN_RIGHT);style.setVerticalAlignment(HSSFCellStyle.VERTICAL_BOTTOM);row.getCell(2).setCellStyle(style);//左上//设置字体大小颜色style = row.getCell(1).getCellStyle();//取出中中的样式HSSFFont font = wb.createFont();font.setFontName("方正姚体");font.setFontHeightInPoints((short)30);font.setItalic(true);//斜体font.setColor(HSSFColor.RED.index);style.setFont(font);//字体旋转style = row.getCell(1).getCellStyle();style.setRotation((short)-30);/** * 设置单元格格式---设置边框 *  */row = sheet.createRow(2);cell = row.createCell(0);style = wb.createCellStyle();style.setBorderTop(HSSFCellStyle.BORDER_DOUBLE);//双实线style.setTopBorderColor(HSSFColor.BLUE.index);//style.setFillForegroundColor(HSSFColor.GOLD.index);cell.setCellStyle(style);/** * 单元格计算 */row = sheet.createRow(3);row.createCell(0).setCellValue(3);row.createCell(1).setCellValue(21);row.createCell(2).setCellValue(11);row.createCell(3).setCellFormula("sum(A4:C4)");//移动行sheet.shiftRows(1, 3, 2);//二三四向下串两行//拆分窗格//1000:左侧窗格的宽度;2000:上侧窗格的宽度;3:右侧窗格开始显示列的索引;4:下侧窗格开始显示行的索引;0:激活的面板区//sheet.createSplitPane(1000, 2000, 3, 4, 0);//冻结窗格//1:左侧冻结的列数;2:上侧冻结的行数;3:右侧窗格开始显示列的索引;4:下侧窗格开始显示行的索引;sheet.createFreezePane(1, 2, 3 ,4);//保存wb.write(new FileOutputStream(new File("H:/poi.xls")));}}



                                             
0 0
原创粉丝点击