简单的读写到excel中:

  1. import java.io.FileNotFoundException;
  2. import java.io.FileOutputStream;
  3. import java.io.IOException;
  4. import java.io.OutputStream;
  5.  
  6. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  7. import org.apache.poi.ss.usermodel.Cell;
  8. import org.apache.poi.ss.usermodel.Row;
  9. import org.apache.poi.ss.usermodel.Sheet;
  10. import org.apache.poi.ss.usermodel.Workbook;
  11.  
  12. public class CreateEx {
  13. /**
  14. *
  15. * 层次结构就是workbook-->Sheet-->Row-->Cell
  16. * 只要按照这种层次结构操作就不会有什么大的问题
  17. * @author Administrator
  18. * @throws IOException
  19. *
  20. */
  21. public static void main(String[] args) throws IOException {
  22. //HSSFWorkbook对应的是2003
  23. //XSSFWorkbook对应的是2007
  24. //对于03和07它们的操作都是差不多的,只是在需要用07的时候把相应的HSSF前缀改成XSSF前缀就可以了
  25. //第一步建一个工作簿,即workbook
  26. Workbook workbook = new HSSFWorkbook();
  27. //第二步建一个工作表单,急sheet
  28. Sheet sheet = workbook.createSheet("mysheet1");
  29. for (int i=0;i<5;i++) {
  30. //有了表单以后就是行Row了,
  31. Row row = sheet.createRow(i);
  32. for (int j=0;j<5;j++) {
  33. //有了row以后就是row上的一个个小的单元格了
  34. Cell cell = row.createCell(j);
  35. //给单元格添加内容
  36. cell.setCellValue("row"+(i+1)+",column"+(j+1));
  37. }
  38. }
  39. //建一个用于存放新建的excel的文件输出流
  40. OutputStream os = new FileOutputStream("f://2.xls");
  41. //把形成的workbook写到一个输出流里面
  42. workbook.write(os);
  43. os.close();
  44. }
  45. }

合并单元格:

  1. public static void main(String args[]) throws IOException {
  2. HSSFWorkbook wb = new HSSFWorkbook();
  3. HSSFSheet sheet = wb.createSheet("sheet1");
  4. HSSFRow row = sheet.createRow(1);
  5. HSSFCell cell = row.createCell(1);
  6. cell.setCellValue("a test of merge!");
  7. //执行合并操作的语句
  8. sheet.addMergedRegion(new CellRangeAddress(
  9. 1,// 开始行
  10. 1,// 结束行
  11. 1,// 开始列
  12. 3// 结束列
  13. ));
  14. OutputStream os = new FileOutputStream("f://3.xls");
  15. wb.write(os);
  16. os.close();
  17. }

换行:

  1. public static void main(String args[]) throws IOException {
  2. HSSFWorkbook wb = new HSSFWorkbook();
  3. HSSFSheet sheet = wb.createSheet();
  4. HSSFRow row = sheet.createRow(6);
  5. sheet.autoSizeColumn(2);
  6. for (int i=0;i<5;i++) {
  7. HSSFCell cell = row.createCell(i+2);
  8. HSSFCellStyle style = wb.createCellStyle();
  9. //to set cell newLine should set its wrap true
  10. style.setWrapText(true);
  11. //利用\n来实现换行操作,只有在Cell设置为setWrapText(true)的时候才能实现人为的换行
  12. cell.setCellValue("just use \n to wrap in a cell!");
  13. cell.setCellStyle(style);
  14. }
  15. OutputStream os = new FileOutputStream("f://4.xls");
  16. wb.write(os);
  17. os.close();
  18. }

画图:

  1. public static void main(String[] args) throws IOException {
  2. //drawing shapes
  3. /*
  4. * To create a shape you have to go through the following steps:
  5.  
  6. 1.Create the patriarch.
  7. 2.Create an anchor to position the shape on the sheet.
  8. 3.Ask the patriarch to create the shape.
  9. 4.Set the shape type (line, oval, rectangle etc...)
  10. 5.Set any other style details converning the shape. (eg: line thickness, etc...)
  11.  
  12. */
  13. HSSFWorkbook wb = new HSSFWorkbook();
  14. HSSFSheet sheet = wb.createSheet();
  15. HSSFPatriarch partriarch = (HSSFPatriarch) sheet.createDrawingPatriarch();
  16. HSSFSimpleShape shape = partriarch.createSimpleShape(new HSSFClientAnchor(0,0,0,0,(short)3,3,(short)5,5));
  17. shape.setShapeType(HSSFSimpleShape.OBJECT_TYPE_OVAL);
  18. //shape可以设置很多的属性
  19. shape.setFillColor(255,200,200);
  20. shape.setLineStyle(HSSFSimpleShape.LINESTYLE_DASHGEL);
  21. //Text boxes are created using a different call:
  22. OutputStream os = new FileOutputStream("f://5.xls");
  23. wb.write(os);
  24. os.close();
  25.  
  26. }

POI创建生成excel及设置相关属性的更多相关文章

  1. poi生成excel整理(设置边框/字体/颜色/加粗/居中/)

    转: poi生成excel整理(设置边框/字体/颜色/加粗/居中/) 2016年12月02日 11:05:23 吃奶的牛 阅读数:34324   HSSFWorkbook wb = new HSSFW ...

  2. 【POI】使用POI 创建生成XLS,打开xls文件提示【此文件中某些文本格式可能已经更改,因为它已经超出最多允许的字体数。】

    使用POI 创建生成XLS,打开xls文件提示[此文件中某些文本格式可能已经更改,因为它已经超出最多允许的字体数.] 原因: 是因为在POI处理xls的过程中,太多次调用了: HSSFFont fon ...

  3. asp.net+nopi生成Excel遇到设置单元格值null问题

    Npoi 生成excel报表功能很不错,功能也不用给大家介绍了.首先看遇到的问题吧! FileStream file = new FileStream(Server.MapPath("Tem ...

  4. 使用poi实现生成excel文件

    import java.util.ArrayList; import javax.servlet.ServletOutputStream; import org.apache.poi.hssf.use ...

  5. VBA 打印设置相关属性及方法

    打印设置说明,以下均为默认值. With ActiveSheet.PageSetup .PrintTitleRows = "" '工作表打印标题:顶端标题行(R) .PrintTi ...

  6. 工作总结 @Html 辅助方法 为 生成的 标签设置元素属性 htmlAttributes 一个对象,其中包含要为该元素设置的 HTML 特性。

    @Html.RadioButtonFor(m => m.IsJianChe, true, new { @style = "width: 18px; height: 18px;" ...

  7. Struts2使用POI创建Excel并下载

    本文将讲解在Struts2框架下如何使用POI创建Office Excel文档并实现下载功能. Apache POI ,操作微软文档的Java API,简单来说就是可以用来操作Office文档的API ...

  8. poi自动生成Ecxel表格和Chart图表

    最近因为业务需求,需要做poi自动导出Ecxel表格和Chart折线图的功能. 所以我在网上找到了一篇关于poi生成Chart图表的博客,代码很详细,但是缺少相关注释说明. 想要将它改造成自己需要的样 ...

  9. springMVC(4)---生成excel文件并导出

    springMVC(4)---生成excel文件并导出 在开发过程中,需要将数据库中的数据以excel表格的方式导出. 首先说明.我这里用的是Apache的POI项目,它是目前比较成熟的HSSF接口, ...

随机推荐

  1. LCT 动态树 模板

    洛谷:P3690 [模板]Link Cut Tree (动态树) /*诸多细节,不注意就会调死去! 见注释.*/ #include<cstdio> #include<iostream ...

  2. 笔记-JavaWeb学习之旅6

    表格标签: table:定义表格 width :宽度 border:边框 cellpadding:定义内容和单元格的距离了 cellspacing:定义单元格之间的距离 bgcolor:背景色 tr: ...

  3. assembly x86(nasm)画三角形等图形的实现(升级版)

    https://www.cnblogs.com/lanclot-/p/10962702.html接上一篇 本来就有放弃的想法,可是有不愿退而求次, 然后大神室友写了一个集海伦公式计算三角形面积, 三点 ...

  4. idea下载

  5. excel输入值非法,限定了可以输入的数值怎么办

    回到excel的编辑界面,点击工具栏的“数据”标签,如图所示. 继续在“数据”标签的下面找到“数据验证”或“数据有效性”的按钮,点击该选项,然后继续下一步. 在弹出的选择框中选择“数据验证”选项,如图 ...

  6. Django 使用Paginator分页

    from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger subclass_s = models.subclas ...

  7. python学习之序列化

    序列化:不同编程语言之间传递对象需要序列化成标准格式,有XML /JSON,json格式化为字符串,UTF-8编码,速度快,切实标准格式.JSON 和 Python内置的数据类型对应如下: JSON ...

  8. python 多继承(新式类) 二

    在python中,要调用父类的某个方法,python2.2之前需要如下代码: class A:def __init__(self):   print "enter A"   pri ...

  9. JavaScriptSerializer类序列化日期时需要注意的问题

    1.让我们来看看使用JavaScriptSerializer类序列化日期会出现什么问题? 1)创建用于序列化的测试类,如下: public class Person { public int ID { ...

  10. android开发学习 ------- 上传本地项目到gitlab

    写了一个demo,上传到gitlab 参考  https://blog.csdn.net/litianxiang_kaola/article/details/74075151 1:新建项目,填写相应的 ...