java excel 操作

news/2024/7/21 3:56:37 标签: excel, Excel, EXCEL, poi, POI

   归纳总结 java EXCEL.html" title=excel>excel 操作:

1.设置不显示EXCEL.html" title=excel>excel网格线 
  sheet.setDisplayGridlines(false);其中sheet是Sheet对象 
2.设置EXCEL.html" title=excel>excel单元格中的内容换行 
  cellStyle.setWrapText(true);其中cellStyle是WorkBook创建的CellStyle对象,然后将cellStyle设置到要换行的Cell对象,最后在要换行的对象(一般为字符串)加入"/r/n"。如 
topTile.append("/r/n" +"cellContent"); 

3.单元格的合并 
  sheet.addMergedRegion(new CellRangeAddress(0, 4, 0, 2));本示例为合并4行2列 

4.设置页眉和页脚的页数 
    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"); 

  HSSFFooter footer = (HSSFFooter )sheet.getFooter() 
  footer.setRight( "Page " + HSSFFooter.page() + " of " + HSSFFooter.numPages() ); 

5.使得一个Sheet适合一页 
  sheet.setAutobreaks(true); 
6.设置放大属性(Zoom被明确为一个分数,例如下面的75%使用3作为分子,4作为分母) 
  sheet.setZoom(3,4);   

7.设置打印 
  HSSFPrintSetup print = (HSSFPrintSetup) sheet.getPrintSetup(); 
  print.setLandscape(true);//设置横向打印 
  print.setScale((short) 70);//设置打印缩放70% 
  print.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE);//设置为A4纸张 
  print.setLeftToRight(true);//設置打印顺序先行后列,默认为先列行            
  print.setFitHeight((short) 10);设置缩放调整为10页高 
  print.setFitWidth((short) 10);设置缩放调整为宽高 

  sheet.setAutobreaks(false); 
  if (i != 0 && i % 30 == 0) 
      sheet.setRowBreak(i);//設置每30行分頁打印 

8.反复的行和列(设置打印标题) 
  HSSFWorkbook wb = new HSSFWorkbook(); 
  wb.setRepeatingRowsAndColumns(0, 0, 12, 1, 6);//设置1到12列,行1到6每一页重复打印 

9.调整单元格宽度 
  sheet.setAutobreaks(true); 
  sheet.setColumnWidth((short)i,colsWidth[i]); //设定单元格长度 
    sheet.autoSizeColumn((short) i);//自动根据长度调整单元格长度


java POI EXCEL.html" title=Excel>Excel样式

下面是一个创建单元格样式的示例

/**

* 单元格标题样式

* */

private static CellStyle getOrderEXCEL.html" title=Excel>ExcelTitleStyle(Workbook workBook){

CellStyle cellStyle = workBook.createCellStyle();

cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); 

cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

// 设置单元格字体

Font headerFont = workBook.createFont(); // 字体

headerFont.setFontHeightInPoints((short)12);

headerFont.setFontName("宋体");

cellStyle.setFont(headerFont);

cellStyle.setWrapText(true);


//填充的背景颜色 

cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

HSSFPalette palette = ((HSSFWorkbook) workBook).getCustomPalette();

palette.setColorAtIndex(HSSFColor.LIME.index, (byte)147, (byte)205,(byte)221);

titleStyle.setFillForegroundColor(HSSFColor.LIME.index);//前景色

titleStyle.setFillBackgroundColor(HSSFCellStyle.THICK_FORWARD_DIAG);

 

// 设置单元格边框及颜色

cellStyle.setBorderBottom((short)1);

cellStyle.setBorderLeft((short)1);

cellStyle.setBorderRight((short)1);

cellStyle.setBorderTop((short)1);

cellStyle.setWrapText(true);

HSSFPalette cellPalette = ((HSSFWorkbook) workBook).getCustomPalette(); // 创建颜色 这里创建的是绿色边框

cellPalette.setColorAtIndex(HSSFColor.GREEN.index, (byte)116, (byte)210,(byte)129); // 设置 RGB

cellStyle.setLeftBorderColor(HSSFColor.GREEN.index); // 设置边框颜色

cellStyle.setRightBorderColor(HSSFColor.GREEN.index);

cellStyle.setTopBorderColor(HSSFColor.GREEN.index);

cellStyle.setBottomBorderColor(HSSFColor.GREEN.index);

return cellStyle;

}



http://www.niftyadmin.cn/n/1696855.html

相关文章

Python文件操作(I/O):从相关函数到pickle模块

Python文件操作(I/O):从相关函数到pickle模块 相关函数和pickle模块Python文件操作(I/O):从相关函数到pickle模块一. tell() 和 seek() 函数tell()函数seek()函数二.with as 语句三.with as底层原理上下文管理器基于类的上下文管理…

Python文件操作(I/O):相关模块(一)

Python文件操作(I/O):相关模块(一) 从fileinput模块到pathlib模块Python文件操作(I/O):相关模块(一)一.fileinput模块input()函数fileinput 模块常用函数二.linecache模块linecache模…

[SDOI2013]森林

主席树上树 写起来有点麻烦 这题两个操作 一个是查找路径上的第k大 一个是连边 首先处理树上路径第k大 如果要找\(u->v\)的第k大 那么一棵主席树u表示的是点u到根的每个权值出现了几次 所以求树上路径第k大就可以直接用\(T_u T_v - T_{lca(u,v)} - T_{fa(lca(u,v))}\) 这个…

Python文件操作(I/O):相关模块(二)

Python文件操作(I/O):相关模块(二) 从os.path模块到tempfile模块Python文件操作(I/O):相关模块(二)一.os.path模块os.path模块常用函数二.fnmatch模块fnmatch模块常用函数三.tempfil…

java锁的简化

java使用单独的锁对象的代码展示 private Lock bankLock new ReentrantLock(); //因为sufficientFunds是锁创建的条件所以称其为条件对象也叫条件变量。 private Condition sufficientFunds bankLock.newCondition();public void transfer(int from, int to, double amount) …

发邮件-1job.xml

job.xm配置 <?xml version1.0 encodingutf-8?> <quartz> <!-- 监控 --> <job> <job-detail> <name>AutoMonitorLogrecGenerateJob</name> <group>AutoMonitorLogrecGenerateGro…

Python爬虫学习笔记(一):从robots协议开始

Python爬虫学习笔记&#xff08;一&#xff09;&#xff1a;从robots协议开始 学习爬虫前的准备工作Python爬虫学习笔记&#xff08;一&#xff09;&#xff1a;从robots协议开始一.关于爬虫和robots协议robots协议robots.txt语法二.HTTP&#xff0c;HTTPS协议URLHTTP协议&…

学习Hibernate的(笔者一共会写四部分)

今天开始学习SSH框架&#xff0c;虽然在网上看到有人评论说SSH不火了&#xff0c;学习SSM吧。 但是&#xff0c;我这个人比较老实&#xff08;哈哈哈&#xff09; 先从SSH开始吧&#xff08;一起学习&#xff0c;一起努力&#xff09; 1、今天学习了Hibernate的环境配置&#…