//创建工作薄(excel)
Workbook wb = new HSSFWorkbook();
//创建sheet
Sheet createSheet = wb.createSheet("sheet1"); //设置标题字体
Font fontTitle = wb.createFont();
fontTitle.setFontHeightInPoints((short) 18); //字体大小
fontTitle.setColor(HSSFColor.BLACK.index); //字体颜色
fontTitle.setFontName("宋体"); //字体
fontTitle.setBoldweight(Font.BOLDWEIGHT_BOLD); //粗体显示
//font.setItalic(true); //是否使用斜体
//font.setStrikeout(true); //是否使用划线 //设置标题单元格类型
CellStyle cellStyleTitle = wb.createCellStyle();
cellStyleTitle.setFont(fontTitle);
cellStyleTitle.setFillForegroundColor(IndexedColors.LIME.getIndex());
cellStyleTitle.setFillPattern(CellStyle.SOLID_FOREGROUND);
cellStyleTitle.setAlignment(CellStyle.ALIGN_CENTER); //水平布局:居中
cellStyleTitle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
cellStyleTitle.setWrapText(true);//设置自动换行 cellStyleTitle.setBorderBottom(CellStyle.BORDER_THIN); //下边框
cellStyleTitle.setBorderLeft(CellStyle.BORDER_THIN);//左边框
cellStyleTitle.setBorderTop(CellStyle.BORDER_THIN);//上边框
cellStyleTitle.setBorderRight(CellStyle.BORDER_THIN);//右边框
cellStyleTitle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
cellStyleTitle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
cellStyleTitle.setTopBorderColor(IndexedColors.BLACK.getIndex());
cellStyleTitle.setRightBorderColor(IndexedColors.BLACK.getIndex()); //创建合并单元格 ---begin
CellRangeAddress region = new CellRangeAddress(0, 0, 1, 3);// 下标从0开始 起始行号,终止行号, 起始列号,终止列号
CellRangeAddress region2 = new CellRangeAddress(1, 2, 0, 0);// 起始行号,终止行号, 起始列号,终止列号
CellRangeAddress region3 = new CellRangeAddress(1, 1, 1, 3);// 起始行号,终止行号, 起始列号,终止列号
//在sheet里增加合并单元格
createSheet.addMergedRegion(region);
createSheet.addMergedRegion(region2);
createSheet.addMergedRegion(region3); // -----------填充第一行数据-------------
Row rowTitle = createSheet.createRow(0);
Cell cellTitle = rowTitle.createCell(0);
cellTitle.setCellStyle(cellStyleTitle);
cellTitle.setCellValue("");// 设置标题内容
Cell cellTitle1_1 = rowTitle.createCell(1);
cellTitle1_1.setCellStyle(cellStyleTitle);
cellTitle1_1.setCellValue(dateStr);// 设置标题内容
// -----------填充第二行数据-------------
Row rowTitle1 = createSheet.createRow(1);
Cell cellTitle1 = rowTitle1.createCell(0);
cellTitle1.setCellStyle(cellStyleTitle);
cellTitle1.setCellValue("店名");// 设置内容
Cell cellTitle11 = rowTitle1.createCell(1);
cellTitle11.setCellStyle(cellStyleTitle);
cellTitle11.setCellValue("王总");//
Cell cellTitle111 = rowTitle1.createCell(2);
cellTitle111.setCellStyle(cellStyleTitle);
cellTitle111.setCellValue("");// 虽然这个单元格不可以不设置,但是要给它设置样式,所以也写了
Cell cellTitle1111 = rowTitle1.createCell(3);
cellTitle1111.setCellStyle(cellStyleTitle);
cellTitle1111.setCellValue("");// 虽然这个单元格不可以不设置,但是要给它设置样式,所以也写了
// -----------填充第三行数据-------------
Row rowTitle2 = createSheet.createRow(2);
Cell cellTitle2 = rowTitle2.createCell(1);
cellTitle2.setCellStyle(cellStyleTitle);
cellTitle2.setCellValue("装修费");// 设置内容
Cell cellTitle22 = rowTitle2.createCell(2);
cellTitle22.setCellStyle(cellStyleTitle);
cellTitle22.setCellValue("加盟费");// 设置内容
Cell cellTitle222 = rowTitle2.createCell(3);
cellTitle222.setCellStyle(cellStyleTitle);
cellTitle222.setCellValue("人员培训费");// 设置内容
// 合并单元格 ----end
// 第四行数据留着下面写 //设置表头字体
Font fontHead = wb.createFont();
fontHead.setFontHeightInPoints((short) 14); //字体大小
fontHead.setColor(Font.COLOR_NORMAL); //字体颜色
fontHead.setFontName("Microsoft Sans Serif"); //字体
fontHead.setBoldweight(Font.BOLDWEIGHT_BOLD); //粗体显示
//font.setItalic(true); //是否使用斜体
//font.setStrikeout(true); //是否使用划线 //设置表头单元格类型
CellStyle cellStyleHead = wb.createCellStyle();
cellStyleHead.setFont(fontHead);
cellStyleHead.setAlignment(CellStyle.ALIGN_CENTER); //水平布局:居中
cellStyleHead.setFillForegroundColor(IndexedColors.LIME.getIndex());
cellStyleHead.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
cellStyleHead.setWrapText(true);//设置自动换行 cellStyleHead.setBorderBottom(CellStyle.BORDER_THIN); //下边框
cellStyleHead.setBorderLeft(CellStyle.BORDER_THIN);//左边框
cellStyleHead.setBorderTop(CellStyle.BORDER_THIN);//上边框
cellStyleHead.setBorderRight(CellStyle.BORDER_THIN);//右边框
cellStyleHead.setBottomBorderColor(IndexedColors.BLACK.getIndex());
cellStyleHead.setLeftBorderColor(IndexedColors.BLACK.getIndex());
cellStyleHead.setTopBorderColor(IndexedColors.BLACK.getIndex());
cellStyleHead.setRightBorderColor(IndexedColors.BLACK.getIndex()); //创建第一行,标题
Row row = createSheet.createRow(3);
String[] cellHead = {"", "","", ""};
// 设置列宽
double[] titleWidth = {10, 24, 24, 24};
for (int i = 0; i < cellHead.length; i++) {
Cell createCell = row.createCell(i);
createCell.setCellValue(cellHead[i]);
createCell.setCellStyle(cellStyleHead);
} //设置内容字体
Font fontData = wb.createFont();
fontData.setFontHeightInPoints((short) 14); //字体大小
fontData.setColor(Font.COLOR_NORMAL); //字体颜色
fontData.setFontName("Microsoft Sans Serif"); //字体
//font.setBoldweight(Font.BOLDWEIGHT_BOLD); //粗体显示
//font.setItalic(true); //是否使用斜体
//font.setStrikeout(true); //是否使用划线 //设置内容单元格类型
CellStyle cellStyleDataOdd = wb.createCellStyle();
cellStyleDataOdd.setFont(fontData);
cellStyleDataOdd.setFillPattern(CellStyle.SOLID_FOREGROUND);
cellStyleDataOdd.setAlignment(CellStyle.ALIGN_CENTER); //水平布局:居中
cellStyleDataOdd.setWrapText(true); cellStyleDataOdd.setBorderBottom(CellStyle.BORDER_THIN); //下边框
cellStyleDataOdd.setBorderLeft(CellStyle.BORDER_THIN);//左边框
cellStyleDataOdd.setBorderTop(CellStyle.BORDER_THIN);//上边框
cellStyleDataOdd.setBorderRight(CellStyle.BORDER_THIN);//右边框
cellStyleDataOdd.setBottomBorderColor(IndexedColors.BLACK.getIndex());
cellStyleDataOdd.setLeftBorderColor(IndexedColors.BLACK.getIndex());
cellStyleDataOdd.setTopBorderColor(IndexedColors.BLACK.getIndex());
cellStyleDataOdd.setRightBorderColor(IndexedColors.BLACK.getIndex()); try {
int no = 1;
for (int i = 0; i < datas.length(); i++) {
JSONObject dayData = datas.getJSONObject(i);
String date = dayData.optString("date");
JSONArray detail = dayData.getJSONArray("detail"); for (int k = 0; k < detail.length(); k++) {
JSONObject ss = detail.getJSONObject(k); row = createSheet.createRow(k + 4);
int j = 0;
// 店名
Cell cell1 = row.createCell(j++);
cell1.setCellValue(ss.optString("xxxxx"));
cell1.setCellStyle(cellStyleDataOdd);
// 装修费
Cell cell2 = row.createCell(j++);
cell2.setCellValue(ss.optString("xxxxx"));
cell2.setCellStyle(cellStyleDataOdd);
// 加盟费
Cell cell7 = row.createCell(j++);
cell7.setCellValue(ss.optString("xxxxxx"));
cell7.setCellStyle(cellStyleDataOdd);
// 人员培训费
Cell cellH = row.createCell(j++);
cellH.setCellValue(ss.optString("xxxxxx"));
cellH.setCellStyle(cellStyleDataOdd);
}
} } catch (JSONException e) {
e.printStackTrace();
} // 设置列宽
for (int i = 0; i < titleWidth.length; i++) {
createSheet.setColumnWidth((short) i, (short) titleWidth[i] * 256);
} // 设置上面四行冻结
createSheet.createFreezePane( 0, 4, 1, 4); // 前两个参数是你要用来拆分的列数和行数。后两个参数是下面窗口的可见象限,其中第三个参数是右边区域可见的左边列数,第四个参数是下面区域可见的首行

poi的合并单元格和冻结行列的更多相关文章

  1. poi读取合并单元格

    poi读取合并单元格 学习了:http://blog.csdn.net/ycb1689/article/details/9764191 进行了列合并单元格的修正:原来是我自己找错了地方: import ...

  2. poi excel 合并单元格

    结论:final CellRangeAddress cra = new CellRangeAddress(rowId, rowId + rowSkip,        colId, colId + c ...

  3. poi获取合并单元格内的第一行第一列的值

    当读取如图所示的excel时,显示为第1行 第1列 的内容是:合并单元格 其它在合并单元格区域内的单元格不显示 示例代码如下: import java.io.FileInputStream; impo ...

  4. Java导出Excel表,POI 实现合并单元格以及列自适应宽度(转载)

    POI是apache提供的一个读写Excel文档的开源组件,在操作excel时常要合并单元格,合并单元格的方法是: sheet.addMergedRegion(new CellRangeAddress ...

  5. POI 实现合并单元格以及列自适应宽度

    POI是apache提供的一个读写Excel文档的开源组件,在操作excel时常要合并单元格,合并单元格的方法是: sheet.addMergedRegion(new CellRangeAddress ...

  6. POI 简单合并单元格

    public class MergedCells { /** 测试使用的POI版本是3.1 * @param args */ public static void main(String[] args ...

  7. poi导出Excel报表多表头双层表头、合并单元格

    效果图: controller层方法: /**     *      * 导出Excel报表     * @param request     * @return     *      */    @ ...

  8. java poi 合并单元格后边框问题

    在项目中用poi合并单元格,但发现边框会有不显示的问题. 在网上搜集了答案,来记录一下. 解决方法: 将每个没用到的单元格都设空值. 例如: HSSFCell cell = row.createCel ...

  9. poi 合并单元格、设置边框

    HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet(); //创建一个样式 HSSFCellStyle sty ...

随机推荐

  1. 单向循环链表C语言实现

    我们都知道,单向链表最后指向为NULL,也就是为空,那单向循环链表就是不指向为NULL了,指向头节点,所以下面这个程序运行结果就是,你将会看到遍历链表的时候就是一个死循环,因为它不指向为NULL,也是 ...

  2. 为什么选择Nginx

     Nginx 是一个高性能的 Web 和反向代理服务器, 它具有有很多非常优越的特性: 作为 Web 服务器:相比 Apache,Nginx 使用更少的资源,支持更多的并发连接,体现更高的效率,这 ...

  3. cocos2d-x action执行完毕的回调

    cocos2d-x action执行完毕的回调 MySprite::createOne() { .... //  MUST add to sheet firstly        spriteShee ...

  4. Oracle UTL_HTTP(收集汇总有用资料)

    From Oracle The UTL_HTTP package makes Hypertext Transfer Protocol (HTTP) callouts from SQL and PL/S ...

  5. OAF隐藏显示题头

    当一个页面的内容较多时,我们需要将内容进行分组显示.这个时候我们就需要用到隐藏显示. 本文将介绍如何使用OAF的隐藏显示题头功能 一.新建AM 在test.oracle.apps.cux上点右键,选择 ...

  6. hadoop的节点间的通信

    一个DataNode上的Block是唯一的,多个DataNode可能有相同的Block. 2)通信场景: (1)NameNode的映射表上不永久保存每个DataNode所对应的block信息,而是通过 ...

  7. C++之继承

    #include <iostream> using namespace std ; class Animal { private: int age ; protected: int id ...

  8. gdb中的define命令

    可以使用define命令达到类似于display的效果:比如每ni后显示当前5条指令: define s5 ni x/5i $pc end 甚至可以手动传入要显示指令的条数: define s ni ...

  9. obj-c编程17:键值观察(KVO)

    说完了前面一篇KVC,不能不说说它的应用KVO(Key-Value Observing)喽.KVO类似于ruby里的hook功能,就是当一个对象属性发生变化时,观察者可以跟踪变化,进而观察或是修正这个 ...

  10. iOS苹果自带UIMenuController

    一.UIMenuController认识 1.默认情况下,UITextView / UITextFiled / UIWebView 都有苹果自带的有UIMenuController功能 2.UITex ...