POI
来源:互联网 发布:js 设置body style 编辑:程序博客网 时间:2024/06/04 18:48
下载地址:demo
package poi;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.PrintStream;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.ss.usermodel.BorderStyle;import org.apache.poi.ss.usermodel.FillPatternType;import org.apache.poi.ss.usermodel.HorizontalAlignment;import org.apache.poi.ss.usermodel.IndexedColors;import org.apache.poi.ss.usermodel.VerticalAlignment;import org.apache.poi.ss.util.CellRangeAddress;/** * * @author bxh * * HSSFWorkbook 工作薄 * HSSFSheet sheet * HSSFRow 单元行 * HSSFCell 单元格 * * * HSSFDataFormat 日期格式 * BorderStyle 边框风格 * IndexedColors 颜色枚举 */public class MyExcel { private final String fileName="c://debug.xls"; private HSSFWorkbook wb; private HSSFSheet sheet; private HSSFCellStyle style; public MyExcel() { File file = new File(fileName); if(file.exists()){ FileInputStream io; try { io = new FileInputStream(file); wb = new HSSFWorkbook(io); sheet = wb.getSheetAt(0); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }else{ wb = new HSSFWorkbook(); sheet = wb.createSheet("sheet1"); initTitle(); } } /** * excle标题 */ private void initTitle() { HSSFRow titleRow = sheet.createRow(0); HSSFCell cellName = titleRow.createCell(0); HSSFCell cellSex = titleRow.createCell(1); HSSFCell cellAge = titleRow.createCell(2); cellName.setCellValue("姓名"); cellSex.setCellValue("性别"); cellAge.setCellValue("年龄"); initStyle(); cellName.setCellStyle(style); cellSex.setCellStyle(style); cellAge.setCellStyle(style); } /** * 写出excel文件 */ public void writeToExcel(){ PrintStream bookOut; try { bookOut = new PrintStream(fileName); wb.write(bookOut); wb.close(); } catch (IOException e) { e.printStackTrace(); } } /** * 写入单元格数据 * @param s 单元夹 * @param datas 数据 前面数组是 行 后面是数据列 * * {{cell,cell,cell},{cell,cell,cell}} 该记录有两个row 一个row 有3 个cell */ public void createRow(HSSFSheet s ,String datas [][] ){ if(s==null){ s=sheet; } //单元格总行数 int rowNo= s.getLastRowNum()+1; for (int i = 0; i < datas.length; i++) { HSSFRow row = s.createRow(i+rowNo); for (int j = 0; j < datas[i].length; j++) { HSSFCell cell = row.createCell(j); cell.setCellValue(datas[i][j]); } } } /** * 合并单元格 * @param sheet 合并的单元夹 * @param firstRow 首个单元格开始位置 * @param lastRow 最后单元格开始位置 * @param firstCol 首个单元格结束始位置 * @param lastCol 最后单元格结束位置 */ public void MergeExcel(HSSFSheet sheet,int firstRow,int lastRow, int firstCol,int lastCol){ sheet.addMergedRegion( new CellRangeAddress(firstRow, lastRow, firstCol, lastCol)); } public void initStyle(){ style = wb.createCellStyle(); // 设置这些样式 style.setAlignment(HorizontalAlignment.CENTER);//水平居中 style.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中 //背景色 style.setFillPattern(FillPatternType.BIG_SPOTS); style.setFillForegroundColor(IndexedColors.YELLOW.getIndex()); style.setFillBackgroundColor(IndexedColors.YELLOW.getIndex()); //设置边框 style.setBorderBottom(BorderStyle.THIN); style.setBorderLeft(BorderStyle.THIN); style.setBorderRight(BorderStyle.THIN); style.setBorderTop(BorderStyle.THIN); // 生成一个字体 HSSFFont font = wb.createFont(); font.setColor(IndexedColors.RED.getIndex()); font.setFontHeightInPoints((short) 10); font.setFontName("微软雅黑"); font.setBold(true); // 把字体 应用到当前样式 style.setFont(font); //自动换行 style.setWrapText(true); }}
package poi;import java.util.Arrays;import java.util.Collection;import org.junit.Test;import org.junit.runner.RunWith;import org.junit.runners.Parameterized;import org.junit.runners.Parameterized.Parameters;@RunWith(value=Parameterized.class)public class TestMyExcel { private String name ; private String sex; private String age ; public TestMyExcel(String name , String sex, String age) { this.age=age; this.name=name; this.sex=sex; } @Parameters public static Collection testDate(){ return Arrays.asList(new Object[][]{ {"赵6","男","33"}, {"张三","女","21"}, {"李四","女","22"}, {"王五","男","33"} }); } /** * junit 测试入口 */ @Test public void TestWookBook(){ MyExcel myExcel = new MyExcel(); myExcel.createRow(null, new String[][]{{name,sex,age}}); myExcel.writeToExcel(); }}
阅读全文
0 0
- poi
- POI
- POI
- POI
- POI
- poi
- poi
- POI
- POI
- POI
- poi
- POI
- POI
- poi
- POI
- POI
- POI
- POI
- sap scc4 客户端设置
- Tensorflow实现图像分割——FCN模型
- ubuntu 17.04 安装最新版本redis(原来的ppa源失效了,需要修改,包含自动重启)
- Android命名规范
- SGU 134. Centroid(树的重心)
- POI
- JAVA中导出、导入EXCEL用到的方法和一些语句
- 浅析android OCR文字识别--转载
- 文章标题
- [SQL](Function)获取当前日期(二)
- mysql根据需要的时间进行查询
- 指针(一)
- BLDC PWM调试方式介绍
- Qt Quick App的两种启动模式