`

OI 导入导出 Excel(好东西,以后用时再看 哈哈)

阅读更多
1.创建工作簿 (WORKBOOK)
    HSSFWorkbook wb = new HSSFWorkbook();
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
2.创建工作表(SHEET)
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet1 = wb.createSheet("new sheet");
    HSSFSheet sheet2 = wb.createSheet("second sheet");
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
3.创建单元格(CELL)
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("new sheet");
    // Create a row and put some cells in it. Rows are 0 based.
    HSSFRow row = sheet.createRow((short)0);
    // Create a cell and put a value in it.
    HSSFCell cell = row.createCell((short)0);
    cell.setCellValue(1);
    // Or do it on one line.
    row.createCell((short)1).setCellValue(1.2);
    row.createCell((short)2).setCellValue("This is a string");
    row.createCell((short)3).setCellValue(true);
    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
4.创建指定单元格式的单元格
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("new sheet");
    // Create a row and put some cells in it. Rows are 0 based.
    HSSFRow row = sheet.createRow((short)0);
    // Create a cell and put a date value in it.  The first cell is not styled
    // as a date.
    HSSFCell cell = row.createCell((short)0);
    cell.setCellValue(new Date());
    // we style the second cell as a date (and time).  It is important to
    // create a new cell style from the workbook otherwise you can end up
    // modifying the built in style and effecting not only this cell but other cells.
    HSSFCellStyle cellStyle = wb.createCellStyle();
    cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
    cell = row.createCell((short)1);
    cell.setCellValue(new Date());
    cell.setCellStyle(cellStyle);
    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
5. 单元格的不同格式
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("new sheet");
    HSSFRow row = sheet.createRow((short)2);
    row.createCell((short) 0).setCellValue(1.1);
    row.createCell((short) 1).setCellValue(new Date());
    row.createCell((short) 2).setCellValue("a string");
    row.createCell((short) 3).setCellValue(true);
    row.createCell((short) 4).setCellType(HSSFCell.CELL_TYPE_ERROR);
    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
6.单元格的不通对齐方式
    public static void main(String[] args)
            throws IOException
    {
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("new sheet");
        HSSFRow row = sheet.createRow((short) 2);
        createCell(wb, row, (short) 0, HSSFCellStyle.ALIGN_CENTER);
        createCell(wb, row, (short) 1, HSSFCellStyle.ALIGN_CENTER_SELECTION);
        createCell(wb, row, (short) 2, HSSFCellStyle.ALIGN_FILL);
        createCell(wb, row, (short) 3, HSSFCellStyle.ALIGN_GENERAL);
        createCell(wb, row, (short) 4, HSSFCellStyle.ALIGN_JUSTIFY);
        createCell(wb, row, (short) 5, HSSFCellStyle.ALIGN_LEFT);
        createCell(wb, row, (short) 6, HSSFCellStyle.ALIGN_RIGHT);
        // Write the output to a file
        FileOutputStream fileOut = new FileOutputStream("workbook.xls");
        wb.write(fileOut);
        fileOut.close();
    }
  
    private static void createCell(HSSFWorkbook wb, HSSFRow row, short column, short align)
    {
        HSSFCell cell = row.createCell(column);
        cell.setCellValue("Align It");
        HSSFCellStyle cellStyle = wb.createCellStyle();
        cellStyle.setAlignment(align);
        cell.setCellStyle(cellStyle);
    }
7.单元格的边框设置
Working with borders
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("new sheet");
    // Create a row and put some cells in it. Rows are 0 based.
    HSSFRow row = sheet.createRow((short) 1);
    // Create a cell and put a value in it.
    HSSFCell cell = row.createCell((short) 1);
    cell.setCellValue(4);
    // Style the cell with borders all around.
    HSSFCellStyle style = wb.createCellStyle();
    style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    style.setBottomBorderColor(HSSFColor.BLACK.index);
    style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
    style.setLeftBorderColor(HSSFColor.GREEN.index);
    style.setBorderRight(HSSFCellStyle.BORDER_THIN);
    style.setRightBorderColor(HSSFColor.BLUE.index);
    style.setBorderTop(HSSFCellStyle.BORDER_MEDIUM_DASHED);
    style.setTopBorderColor(HSSFColor.BLACK.index);
    cell.setCellStyle(style);
    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
8.填充和颜色设置
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("new sheet");
    // Create a row and put some cells in it. Rows are 0 based.
    HSSFRow row = sheet.createRow((short) 1);
    // Aqua background
    HSSFCellStyle style = wb.createCellStyle();
    style.setFillBackgroundColor(HSSFColor.AQUA.index);
    style.setFillPattern(HSSFCellStyle.BIG_SPOTS);
    HSSFCell cell = row.createCell((short) 1);
    cell.setCellValue("X");
    cell.setCellStyle(style);
    // Orange "foreground", foreground being the fill foreground not the font color.
    style = wb.createCellStyle();
    style.setFillForegroundColor(HSSFColor.ORANGE.index);
    style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    cell = row.createCell((short) 2);
    cell.setCellValue("X");
    cell.setCellStyle(style);
    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
9.合并单元格操作
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("new sheet");
    HSSFRow row = sheet.createRow((short) 1);
    HSSFCell cell = row.createCell((short) 1);
    cell.setCellValue("This is a test of merging");
    sheet.addMergedRegion(new Region(1,(short)1,1,(short)2));
    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
10.字体设置
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("new sheet");
    // Create a row and put some cells in it. Rows are 0 based.
    HSSFRow row = sheet.createRow((short) 1);
    // Create a new font and alter it.
    HSSFFont font = wb.createFont();
    font.setFontHeightInPoints((short)24);
    font.setFontName("Courier New");
    font.setItalic(true);
    font.setStrikeout(true);
    // Fonts are set into a style so create a new one to use.
    HSSFCellStyle style = wb.createCellStyle();
    style.setFont(font);
    // Create a cell and put a value in it.
    HSSFCell cell = row.createCell((short) 1);
    cell.setCellValue("This is a test of fonts");
    cell.setCellStyle(style);
    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
11.自定义颜色
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet();
    HSSFRow row = sheet.createRow((short) 0);
    HSSFCell cell = row.createCell((short) 0);
    cell.setCellValue("Default Palette");
    //apply some colors from the standard palette,
    // as in the previous examples.
    //we'll use red text on a lime background
    HSSFCellStyle style = wb.createCellStyle();
    style.setFillForegroundColor(HSSFColor.LIME.index);
    style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    HSSFFont font = wb.createFont();
    font.setColor(HSSFColor.RED.index);
    style.setFont(font);
    cell.setCellStyle(style);
    //save with the default palette
    FileOutputStream out = new FileOutputStream("default_palette.xls");
    wb.write(out);
    out.close();
    //now, let's replace RED and LIME in the palette
    // with a more attractive combination
    // (lovingly borrowed from freebsd.org)
    cell.setCellValue("Modified Palette");
    //creating a custom palette for the workbook
    HSSFPalette palette = wb.getCustomPalette();
    //replacing the standard red with freebsd.org red
    palette.setColorAtIndex(HSSFColor.RED.index,
            (byte) 153,  //RGB red (0-255)
            (byte) 0,    //RGB green
            (byte) 0     //RGB blue
    );
    //replacing lime with freebsd.org gold
    palette.setColorAtIndex(HSSFColor.LIME.index, (byte) 255, (byte) 204, (byte) 102);
    //save with the modified palette
    // note that wherever we have previously used RED or LIME, the
    // new colors magically appear
    out = new FileOutputStream("modified_palette.xls");
    wb.write(out);
    out.close();
12.读和重写EXCEL文件
    POIFSFileSystem fs      =
            new POIFSFileSystem(new FileInputStream("workbook.xls"));
    HSSFWorkbook wb = new HSSFWorkbook(fs);
    HSSFSheet sheet = wb.getSheetAt(0);
    HSSFRow row = sheet.getRow(2);
    HSSFCell cell = row.getCell((short)3);
    if (cell == null)
        cell = row.createCell((short)3);
    cell.setCellType(HSSFCell.CELL_TYPE_STRING);
    cell.setCellValue("a test");
    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
13.在EXCEL单元格中使用自动换行
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet s = wb.createSheet();
    HSSFRow r = null;
    HSSFCell c = null;
    HSSFCellStyle cs = wb.createCellStyle();
    HSSFFont f = wb.createFont();
    HSSFFont f2 = wb.createFont();
    cs = wb.createCellStyle();
    cs.setFont( f2 );
    //Word Wrap MUST be turned on
    cs.setWrapText( true );
    r = s.createRow( (short) 2 );
    r.setHeight( (short) 0x349 );
    c = r.createCell( (short) 2 );
    c.setCellType( HSSFCell.CELL_TYPE_STRING );
    c.setCellValue( "Use \n with word wrap on to create a new line" );
    c.setCellStyle( cs );
    s.setColumnWidth( (short) 2, (short) ( ( 50 * 8 ) / ( (double) 1 / 20 ) ) );
    FileOutputStream fileOut = new FileOutputStream( "workbook.xls" );
    wb.write( fileOut );
    fileOut.close();
14.数字格式自定义
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("format sheet");
    HSSFCellStyle style;
    HSSFDataFormat format = wb.createDataFormat();
    HSSFRow row;
    HSSFCell cell;
    short rowNum = 0;
    short colNum = 0;
    row = sheet.createRow(rowNum++);
    cell = row.createCell(colNum);
    cell.setCellValue(11111.25);
    style = wb.createCellStyle();
    style.setDataFormat(format.getFormat("0.0"));
    cell.setCellStyle(style);
    row = sheet.createRow(rowNum++);
    cell = row.createCell(colNum);
    cell.setCellValue(11111.25);
    style = wb.createCellStyle();
    style.setDataFormat(format.getFormat("#,##0.0000"));
    cell.setCellStyle(style);
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
15.调整工作单位置
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("format sheet");
    HSSFPrintSetup ps = sheet.getPrintSetup();
    sheet.setAutobreaks(true);
    ps.setFitHeight((short)1);
    ps.setFitWidth((short)1);
    // Create various cells and rows for spreadsheet.
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
16.设置打印区域
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("Sheet1");
    wb.setPrintArea(0, "$A$1:$C$2");
    //sets the print area for the first sheet
    //Alternatively:
    //wb.setPrintArea(0, 0, 1, 0, 0) is equivalent to using the name reference (See the JavaDocs for more details)
    // Create various cells and rows for spreadsheet.
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
17.标注脚注
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("format sheet");
    HSSFFooter footer = sheet.getFooter()
    footer.setRight( "Page " + HSSFFooter.page() + " of " + HSSFFooter.numPages() );

    // Create various cells and rows for spreadsheet.
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
18.使用方便的内部提供的函数
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet1 = wb.createSheet( "new sheet" );
    // Create a merged region
    HSSFRow row = sheet1.createRow( (short) 1 );
    HSSFRow row2 = sheet1.createRow( (short) 2 );
    HSSFCell cell = row.createCell( (short) 1 );
    cell.setCellValue( "This is a test of merging" );
    Region region = new Region( 1, (short) 1, 4, (short) 4 );
    sheet1.addMergedRegion( region );
    // Set the border and border colors.
    final short borderMediumDashed = HSSFCellStyle.BORDER_MEDIUM_DASHED;
    HSSFRegionUtil.setBorderBottom( borderMediumDashed,
        region, sheet1, wb );
    HSSFRegionUtil.setBorderTop( borderMediumDashed,
        region, sheet1, wb );
    HSSFRegionUtil.setBorderLeft( borderMediumDashed,
        region, sheet1, wb );
    HSSFRegionUtil.setBorderRight( borderMediumDashed,
        region, sheet1, wb );
    HSSFRegionUtil.setBottomBorderColor(HSSFColor.AQUA.index, region, sheet1, wb);
    HSSFRegionUtil.setTopBorderColor(HSSFColor.AQUA.index, region, sheet1, wb);
    HSSFRegionUtil.setLeftBorderColor(HSSFColor.AQUA.index, region, sheet1, wb);
    HSSFRegionUtil.setRightBorderColor(HSSFColor.AQUA.index, region, sheet1, wb);
    // Shows some usages of HSSFCellUtil
    HSSFCellStyle style = wb.createCellStyle();
    style.setIndention((short)4);
    HSSFCellUtil.createCell(row, 8, "This is the value of the cell", style);
    HSSFCell cell2 = HSSFCellUtil.createCell( row2, 8, "This is the value of the cell");
    HSSFCellUtil.setAlignment(cell2, wb, HSSFCellStyle.ALIGN_CENTER);
    // Write out the workbook
    FileOutputStream fileOut = new FileOutputStream( "workbook.xls" );
    wb.write( fileOut );
    fileOut.close();
19.在工作单中移动行,调整行的上下位置
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("row sheet");
    // Create various cells and rows for spreadsheet.
    // Shift rows 6 - 11 on the spreadsheet to the top (rows 0 - 5)
    sheet.shiftRows(5, 10, -5);
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
20.选种指定的工作单
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("row sheet");
    sheet.setSelected(true);
    // Create various cells and rows for spreadsheet.
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
21.工作单的放大缩小
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet1 = wb.createSheet("new sheet");
    sheet1.setZoom(3,4);   // 75 percent magnification
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
22.头注和脚注
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("new sheet");
    HSSFHeader header = sheet.getHeader();
    header.setCenter("Center Header");
    header.setLeft("Left Header");
    header.setRight(HSSFHeader.font("Stencil-Normal", "Italic") +
                    HSSFHeader.fontSize((short) 16) + "Right w/ Stencil-Normal Italic font and size 16");
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
//-------------------------------以上实例代码均来自官方网站
//-------------------------------POI中使用的颜色是用颜色索引来实现,如下:
 
//----------------------------------------------------你可以按上面的方法来自定义颜色
 
//---------------------------------------------------用以上的基础知识我们就可以制作复杂的多表头,控制元/
//格样式的操作了,具体的代码考虑到公司资产,所以隐去。
官方网站:http://jakarta.apache.org/poi/
分享到:
评论

相关推荐

    OI-Wiki-export:将 OI-Wiki 导出为印刷质量的 pdf 的工具

    将 OI-Wiki 导出为印刷质量的 pdf 的工具。 下载 可在 中下载每周定期自动构建的 pdf 文件. 导出 我们建议在 Linux 环境下进行导出操作。 首先请安装好以下软件包: TeXLive NodeJS imagemagick libwebp librsvg ...

    oi-wiki-18.12.pdf

    OI Wiki 致⼒于成为⼀个免费开放且持续更新的知识整合站点,⼤家可以在这⾥获取关于 编程竞赛 (competitive programming) 有趣⼜实⽤的知识,我们为⼤家准备了竞赛中的基础知识、常⻅题型、解题思路以及常⽤⼯ ...

    OI-wiki.pdf

    OIWiki是一个面向信息学奥林匹克竞赛(Olympiad in Informatics,简称OI)的在线知识库和协作平台。OIWiki项目组在2021年10月3日发布的文档中,介绍了该项目的简介、如何参与、格式手册以及使用Docker部署OIWiki等...

    fanuc oi 培训资料

    在深入探讨Fanuc Oi系统的培训资料之前,我们先来了解一下Fanuc Oi系统的基本概念。Fanuc Oi系统是日本发那科公司(FANUC Corporation)开发的一款高性能CNC(Computer Numerical Control)控制系统,广泛应用于各种...

    OI比赛经验谈.pdf

    OI比赛经验谈 本文是OI比赛经验谈,作者通过分享自己的OI比赛经验和教训,向读者传授比赛策略和心态调整方法。作者认为,OI比赛的成功不仅取决于实力,还取决于比赛经验和心理素质。作者认为,拥有强大的比赛能力,...

    OI Painter.rar

    【标题】"OI Painter.rar" 是一个专门为OI(奥林匹克信息学)竞赛选手设计的画图软件,它旨在帮助用户在C++编程时更好地理解和展示树、图以及数组的数据结构。这款工具通过图形化的方式,使得复杂的算法逻辑变得更加...

    OI论文资料详细分类合集

    在准备参加奥林匹克信息技术竞赛(OI)的过程中,拥有详尽的论文资料是至关重要的。"OI论文资料详细分类合集"这个资源包含了一系列针对不同领域的学习材料,旨在帮助参赛者全面提高其理论知识和实践能力。以下是对每...

    FANUC OI-MF参数手册

    ### FANUC OI-MF 参数手册核心知识点详解 #### 一、FANUC OI-MF 概述 FANUC OI-MF 是一款先进的数控系统,专为加工中心设计,旨在提供高精度、高效率的加工解决方案。本手册主要介绍了FANUC 0I-F 参数设置与应用...

    OI国家集训队论文集1999~2016

    【标题】"OI国家集训队论文集1999~2016"揭示了这一资源是关于奥林匹克信息学(OI)领域的精选论文集合,时间跨度从1999年至2016年,涵盖了长达18年的学术研究与实践成果。OI,即奥林匹克信息技术竞赛,是国际上一项...

    SAPDOI调用Excel例子.pdf

    DOI技术提供了与Excel文件交互的能力,允许SAP开发人员将SAP数据导入Excel或者将Excel数据导入SAP系统,从而在SAP系统内部实现与Excel的交互。 文档中出现的程序代码部分展现了在SAP系统中使用SAP ABAP语言编写程序...

    UG fanuc oi后处理

    UG(Unigraphics Solutions)是一款广泛应用的计算机辅助设计与制造(CAD/...同时,熟悉Fanuc Oi数控系统的编程规则和指令集也是必不可少的,这将有助于更好地理解和优化UG生成的G代码,确保在实际生产中的高效和安全。

    C++ OI 常用号模板

    C++常用代码模板,对于OI比较实用,有常用算法数据结构

    FANUC oi-MD参数说明书

    FANUC oi-MD参数说明书是FANUC公司出产的机床数控系统的参数设置文档。FANUC是全球领先的数控系统制造商,其产品广泛应用于机床控制系统领域。此说明书详细介绍了FANUC系列0-MODEL D型数控系统的相关参数设置和操作...

    OI之路.pdf

    【OI之路】指的是参与信息学奥林匹克竞赛的过程。信息学奥林匹克竞赛(OI)是针对中学生的编程竞赛,主要包括三个级别:全国青少年信息学奥林匹克联赛(NOIP)、全国青少年信息学奥林匹克(NOI)以及国际信息学...

    我的OI算法总结。原创。

    **标题:“我的OI算法总结。原创。”** 这篇文章的作者分享了他在OI(奥林匹克信息学)竞赛中的算法学习和实践经验,涵盖了图论算法、动态规划(DP)以及基础算法的总结。OI是信息学奥林匹克竞赛的缩写,它涉及到...

    SAPDOI调用Excel例子[收集].pdf

    SAP DOI 是一种功能强大的工具,允许开发者在SAP ABAP程序中直接操作Excel文档,进行数据导入导出、分析或报告生成等任务。通过VBA(Visual Basic for Applications)扩展,可以实现更多自定义功能。 在这个例子中...

    信息学奥赛OI历年真题

    信息学奥赛,全称奥林匹克信息学竞赛(Olympiad in Informatics,简称OI),是一项全球范围内的青少年科技赛事,旨在提升青少年的计算机编程、算法设计、数据结构和问题解决能力。这些压缩包文件包含了从2017年至...

    poi 将多个excel复制到新的excel 的多个sheet页中 并复制所有的样式 包括字体的样式

    poi 将多个excel复制到新的excel 的多个sheet页中 并复制所有的样式 包括字体的样式 背景颜色 单元格宽度 等

    Python-oi有CLI界面的可长期运行的应用进程编辑框架

    Python-oi是一个针对命令行界面(CLI)设计的框架,专用于创建可长期运行的应用进程编辑。这个框架的独特之处在于它结合了CLI的便捷性和持久性,使得开发者能够构建出功能强大且用户友好的命令行应用程序,这些程序...

Global site tag (gtag.js) - Google Analytics