要用到poi创建excel的同志来这里看看,会的给点意见,不会的学习学习,
注:poi的jar包自己到官网下载
package com.poi;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
public class WorkBook {
public static void main(String[] args) {
String outPath = "D:\\输出文档";
WorkBook workBook = new WorkBook();
List<String> emplist = new ArrayList<String>();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < 10; i++) {
sb.append(i+"`");
sb.append("卢水发"+i+"`");
sb.append("158706885 -"+i+"`");
sb.append("男");
sb.append("java软件工程师");
emplist.add(sb.toString());
sb = new StringBuffer();
}
String result = workBook.createWorkBook(emplist,outPath);
System.out.println("程序创建工作本的结果是:"+result);
}
/**
* 工作本
* @param outPath
* @return
*/
public String createWorkBook(List emplist,String outPath){
try {
File file = new File(outPath);
if(!file.exists()){
file.mkdirs();
}
//创建工作簿
Workbook wb = new HSSFWorkbook();
//创建工作表
Sheet sheet1 = wb.createSheet("员工表");
//创建一行
for (int i = 0; i < emplist.size(); i++) {
String emp = (String) emplist.get(i);
String[] emps = emp.split("`");
Row row = sheet1.createRow(i);
System.out.println("第 "+i+" 行。"+emp);
for (int j = 0; j < emps.length; j++) {
sheet1.setColumnWidth(j, 20 * 256);
sheet1.setDefaultRowHeight((short) (2 * 256));
createCell(wb,emps[j], row, (short) j, CellStyle.ALIGN_CENTER, CellStyle.ALIGN_CENTER);
System.out.println("第 "+i+" 行。第"+j+"单元格");
}
}
Sheet sheet2 = wb.createSheet("考勤表");
Sheet sheet3 = wb.createSheet("宝宝表");
FileOutputStream fileOut = new FileOutputStream(file+"\\workbook.xls");
wb.write(fileOut);
fileOut.close();
return "输出"+file+"成功了!";
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "出错了!";
}
//设置表格的对齐样式
private static void createCell(Workbook wb,String cellValue, Row row, short column, short halign, short valign) {
Cell cell = row.createCell(column);
cell.setCellValue(cellValue);
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setAlignment(halign);
cellStyle.setVerticalAlignment(valign);
//设置边框颜色
cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
cellStyle.setBottomBorderColor(IndexedColors.GREEN.getIndex());
cellStyle.setBorderLeft(CellStyle.BORDER_THIN);
cellStyle.setLeftBorderColor(IndexedColors.GREEN.getIndex());
cellStyle.setBorderRight(CellStyle.BORDER_THIN);
cellStyle.setRightBorderColor(IndexedColors.GREEN.getIndex());
cellStyle.setBorderTop(CellStyle.BORDER_THIN);
cellStyle.setTopBorderColor(IndexedColors.GREEN.getIndex());
//设置字体
Font font = wb.createFont();
font.setFontHeightInPoints((short)12);
font.setFontName("Courier New");
font.setItalic(false);
font.setBoldweight((short)2);
cellStyle.setFont(font);
cell.setCellStyle(cellStyle);
}
}
分享到:
相关推荐
apache POI,文件读写 ,excel 对于使用apache poi 解析微软excel的一些文件
使用Apache POI的3.16版实现,Excel模板填充数据导出Excel报表
NULL 博文链接:https://sarin.iteye.com/blog/859163
Apache poi 操作 excel 文件压缩包,亲测可用
使用java Apache poi 根据word模板生成word报表 仅支持docx格式的word文件,大概是word2010及以后版本,doc格式不支持。 使用说明:https://blog.csdn.net/u012775558/article/details/79678701
利用apache包来完成excel导入导出 利用apache包来完成excel导入导出
apache poi 读取 Excel 的 jar 包 博文链接:https://wxinpeng.iteye.com/blog/231895
自己花了几天时间写的基于Apache POI的Java 读取excel文件,功能还是比较齐全
apache软件基金 poi组件 轻松实现,生成excel报表
Apache POI Excel操作 需要的文档,.介绍,相关jar包,maven中的配置等,比较全面
Apache Poi Excel导出 博文链接:https://zhiminsun.iteye.com/blog/1883347
Apache POI导入和导出Excel文件
poi excel poi excel poi excel poi excel poi excel poi excel poi excel poi excel
Poi强大的操作技术方便了我们开发,Apache POI导入数据到Excel电子表格
Apache POI For Java Excel.doc
NULL 博文链接:https://keep-going.iteye.com/blog/1907664
poi jxl 生成EXCEL 报表 POI 用的JAR poi-3.6-20091214.jar jxl 用到的jar jxl-2.6.jar
适用于安卓的Excel读写库,简单易用
利用poi技术生成excel报表和读取word文档内容
Apache poi 根据word模板生成word报表 替换 循环列表 图片,代码调试过了,修改相应的图片位置,word模板路径即可运行