POI进行ExcelSheet的拷贝

学习了:http://www.360doc.com/content/17/0508/20/42823223_652205632.shtml,这个也需要改改

这个:http://blog.csdn.net/wutbiao/article/details/8696446#有些问题

目前格式还是无法拷贝,如果拷贝格式会导致wookbook为空;

  1. package com.srie.excel.controller;
  2. import org.apache.poi.hssf.usermodel.HSSFCell;
  3. import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  4. import org.apache.poi.hssf.usermodel.HSSFRow;
  5. import org.apache.poi.hssf.usermodel.HSSFSheet;
  6. import org.apache.poi.hssf.util.HSSFColor;
  7. import org.apache.poi.ss.util.CellRangeAddress;
  8. public class POIUtils {
  9. /**
  10. * 拷贝Excel行
  11. *
  12. * @param fromsheet
  13. * @param newsheet
  14. * @param firstrow
  15. * @param lastrow
  16. */
  17. @SuppressWarnings("deprecation")
  18. public void copyRows( HSSFSheet fromsheet, HSSFSheet newsheet ) {
  19. int firstrow = fromsheet.getFirstRowNum();
  20. int lastrow = fromsheet.getLastRowNum();
  21.  
  22. if ((firstrow == -1) || (lastrow == -1) || lastrow < firstrow) {
  23. return;
  24. }
  25. // 拷贝合并的单元格
  26. for (int i = 0; i < fromsheet.getNumMergedRegions(); i++) {
  27. CellRangeAddress mergedRegion = fromsheet.getMergedRegion(i);
  28. newsheet.addMergedRegion(mergedRegion);
  29. }
  30. HSSFRow fromRow = null;
  31. HSSFRow newRow = null;
  32. HSSFCell newCell = null;
  33. HSSFCell fromCell = null;
  34. // 设置列宽
  35. for (int i = firstrow; i <= lastrow; i++) {
  36. fromRow = fromsheet.getRow(i);
  37. if (fromRow != null) {
  38. for (int j = fromRow.getLastCellNum(); j >= fromRow.getFirstCellNum(); j--) {
  39. int colnum = fromsheet.getColumnWidth((short) j);
  40. if (colnum > 100) {
  41. newsheet.setColumnWidth((short) j, (short) colnum);
  42. }
  43. if (colnum == 0) {
  44. newsheet.setColumnHidden((short) j, true);
  45. } else {
  46. newsheet.setColumnHidden((short) j, false);
  47. }
  48. }
  49. break;
  50. }
  51. }
  52. // 拷贝行并填充数据
  53. for (int i = 0; i <= lastrow; i++) {
  54. fromRow = fromsheet.getRow(i);
  55. if (fromRow == null) {
  56. continue;
  57. }
  58. newRow = newsheet.createRow(i - firstrow);
  59. newRow.setHeight(fromRow.getHeight());
  60. for (int j = fromRow.getFirstCellNum(); j < fromRow.getPhysicalNumberOfCells(); j++) {
  61. fromCell = fromRow.getCell((short) j);
  62. if (fromCell == null) {
  63. continue;
  64. }
  65. newCell = newRow.createCell((short) j);
  66. HSSFCellStyle cellStyle = fromCell.getCellStyle();
  67.  
  68. // HSSFCellStyle newStyle = newsheet.getWorkbook().createCellStyle();
  69. // newStyle.cloneStyleFrom(cellStyle);
  70. // newCell.setCellStyle(newStyle);
  71.  
  72. // HSSFCellStyle cellStyle2 = newCell.getCellStyle();
  73. // cellStyle2.setFillForegroundColor(cellStyle.getFillForegroundColor());
  74. // cellStyle2.setFillPattern(cellStyle.getFillPattern());
  75. // cellStyle2.setAlignment(cellStyle.getAlignment());
  76. // cellStyle2.setVerticalAlignment(cellStyle.getVerticalAlignment());
  77.  
  78. int cType = fromCell.getCellType();
  79. newCell.setCellType(cType);
  80. switch (cType) {
  81. case HSSFCell.CELL_TYPE_STRING:
  82. newCell.setCellValue(fromCell.getRichStringCellValue());
  83. break;
  84. case HSSFCell.CELL_TYPE_NUMERIC:
  85. newCell.setCellValue(fromCell.getNumericCellValue());
  86. break;
  87. case HSSFCell.CELL_TYPE_FORMULA:
  88. newCell.setCellFormula(fromCell.getCellFormula());
  89. break;
  90. case HSSFCell.CELL_TYPE_BOOLEAN:
  91. newCell.setCellValue(fromCell.getBooleanCellValue());
  92. break;
  93. case HSSFCell.CELL_TYPE_ERROR:
  94. newCell.setCellValue(fromCell.getErrorCellValue());
  95. break;
  96. default:
  97. newCell.setCellValue(fromCell.getRichStringCellValue());
  98. break;
  99. }
  100. }
  101. }
  102. }
  103. public static void main(String[] args) {}
  104. }

POI进行ExcelSheet的拷贝的更多相关文章

  1. LeftoverDataException,依赖包,apache license 2.0

    1. poi3.9 LeftoverDataException org.apache.poi.hssf.record.RecordInputStream$LeftoverDataException: ...

  2. java 实现 excel sheet 拷贝到另一个Excel文件中 poi

    public class CopyExcelSheetToAnotherExcelSheet { public static void main(String[] args) throws FileN ...

  3. java:POI导出excel

    POI是一个开源项目,专用于java平台上操作MS OFFICE,企业应用开发中可用它方便导出Excel. 下面是使用示例: 1.maven中先添加依赖项 <dependency> < ...

  4. POI导出EXCEL经典实现

    1.Apache POI简介 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程式对Microsoft Office格式档案读和写的功能. .NET的开发人员则 ...

  5. Java POI 导出EXCEL经典实现 Java导出Excel

    转自http://blog.csdn.net/evangel_z/article/details/7332535 在web开发中,有一个经典的功能,就是数据的导入导出.特别是数据的导出,在生产管理或者 ...

  6. excel poi 文件导出,支持多sheet、多列自动合并。

    参考博客: http://www.oschina.net/code/snippet_565430_15074 增加了多sheet,多列的自动合并. 修改了部分过时方法和导出逻辑. 优化了标题,导出信息 ...

  7. jxl读写excel, poi读写excel,word, 读取Excel数据到MySQL

    这篇blog是介绍: 1. java中的poi技术读取Excel数据,然后保存到MySQL数据中. 2. jxl读写excel 你也可以在 : java的poi技术读取和导入Excel了解到写入Exc ...

  8. Java中使用poi导入、导出Excel

    一.介绍 当前B/S模式已成为应用开发的主流,而在企业办公系统中,常常有客户这样子要求:你要把我们的报表直接用Excel打开(电信系统.银行系统).或者是:我们已经习惯用Excel打印.这样在我们实际 ...

  9. Java使用POI为Excel打水印,调整列宽并设置Excel只读(用户不可编辑)

    本文介绍在Java语言环境下,使用POI为Excel打水印的解决方案,具体的代码编写以及相关的注意事项. 需求描述: 要求通过系统下载的Excel都带上公司的水印,列宽调整为合适的宽度,并且设置为不可 ...

随机推荐

  1. java中与运算,或运算,异或运算,取反运算

      //与运算 &  规则 :都为1时才为1        System.out.println( 7 & 9);        /*         *  7二进制 0111     ...

  2. 我的spark python 决策树实例

    from numpy import array from pyspark.mllib.regression import LabeledPoint from pyspark.mllib.tree im ...

  3. div向右偏移设置 css让div靠右移一定距离

    转自:https://www.thinkcss.com/shili/1372.shtml div对象盒子向右偏移设置,使用css让div靠右一定距离-div向右移教程实例篇 div向右偏移一定距离,可 ...

  4. linux git保存用户名密码(避免每次push输用户名密码)

    Linux/Unix/Mac 系统 新建一个 ~/.netrc 文件, 将 git 服务器, 用户名以及密码记录在这个文件, 如下所示:   machine your-git-server   log ...

  5. 学习java的方式

  6. Elasticsearch之cur查询索引

    前提, Elasticsearch之curl创建索引库 Elasticsearch之curl创建索引 Elasticsearch之curl创建索引库和索引时注意事项 Elasticsearch之cur ...

  7. Linux运维最佳实践之网站调优

    高性能静态网站: 1.静态页面中针对图片进行浏览器(客户端)缓存,如公共JavaScript(jQuery,jQuery-1.12.1.min.js)进行缓存 2.对网站输入内容压缩(gzip) 3. ...

  8. wppay免登录付费查看隐藏内容/付费资源下载

    WPPAY是一款模板兔开发的免登录的付费查看内容/付费下载资源WordPress插件,WPPAY不需要用户注册登录即可支付查看隐藏内容,把整个流程做到极简.发布文章时要隐藏的内容可以利用短代码: [w ...

  9. 关于KO信息

    最近写大论文查到KO也是可以用于分类的一种信息. 如何使用KEGG进行通路富集http://blog.sciencenet.cn/blog-364884-779116.html kegg 数据库学习笔 ...

  10. Android学习——碎片Fragment的使用

    一.碎片的简单用法(实现在一个活动中添加两个碎片,并让这两个碎片平分活动空间) 1.新建一个FragmentTest项目: 新建一个左侧碎片布局left_fragment.xml,代码如下:(只放置一 ...