概述
先生成二维数组
String path = null;
try {
String[][] abnormalLists1 = new String[exportSocialPersonList.size()][17];
int k = 1;
int i = 0;
for (; i < exportSocialPersonList.size(); i++) {
abnormalLists1[i][0] = String.valueOf(k++);
abnormalLists1[i][1] = exportSocialPersonList.get(i).getCtyName();
abnormalLists1[i][2] = exportSocialPersonList.get(i).getBuildName();
abnormalLists1[i][3] = exportSocialPersonList.get(i).getUnitName();
abnormalLists1[i][4] = exportSocialPersonList.get(i).getHouseName();
abnormalLists1[i][5] = exportSocialPersonList.get(i).getPersonType();
abnormalLists1[i][6] = exportSocialPersonList.get(i).getName();
abnormalLists1[i][7] = exportSocialPersonList.get(i).getMobile();
abnormalLists1[i][8] = exportSocialPersonList.get(i).getIdNbr();
abnormalLists1[i][9] = exportSocialPersonList.get(i).getHouseholdPace();
abnormalLists1[i][10] = exportSocialPersonList.get(i).getHouseholdPaceDetail();
abnormalLists1[i][11] = exportSocialPersonList.get(i).getPolitical();
abnormalLists1[i][12] = exportSocialPersonList.get(i).getNation();
abnormalLists1[i][13] = exportSocialPersonList.get(i).getEducation();
abnormalLists1[i][14] = exportSocialPersonList.get(i).getOccupation();
abnormalLists1[i][15] = exportSocialPersonList.get(i).getCompany();
abnormalLists1[i][16] = exportSocialPersonList.get(i).getIsDog();
}
//path = ExcelUtil.exportPerson(abnormalLists1,contractPath,contractPaths,exportWay);
path = ExcelUtil.exportSocialPerson(abnormalLists1,contractPath,contractPaths,exportWay);
} catch (Exception e) {
e.printStackTrace();
}
return path;
}
```
生成文件
```
public static String exportSocialPerson(String[][] abnormalLists1, String contractPath, String contractPaths, String exportWay) throws IOException {
//创建工作薄
XSSFWorkbook xssfWorkbook=new XSSFWorkbook();
XSSFSheet xssfSheet=xssfWorkbook.createSheet("Sheet1");
//创建表单样式
XSSFCellStyle titleStyle = genTitleStyle(xssfWorkbook);//创建标题样式
XSSFCellStyle contextStyle = genContextStyle(xssfWorkbook);//创建文本样式
XSSFCellStyle contextRedColourStyle = genContextRedColourStyle(xssfWorkbook);//创建文本样式
XSSFCellStyle contextYellowColourStyle = genContextYellowColourStyle(xssfWorkbook);//创建文本样式
//创建Excel
genExcelSocialPerson(xssfSheet,titleStyle,contextStyle, contextRedColourStyle,contextYellowColourStyle,abnormalLists1);
// String local_path = System.getProperty("user.dir");
// //获取当前用户的当前工作目录
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 ");
String dateTime = sdf.format(date);
String path = contractPath+dateTime+exportWay+"人员数据.xls";
//添加上文件的路径,组成文件的绝对路径
File file=new File(path);
//将生成的Excel文件保存到本地
FileOutputStream fileOutputStream=new FileOutputStream(file);
xssfWorkbook.write(fileOutputStream);
//通过流的方式写入到文件中中
fileOutputStream.close();
//使用完成之后要记得关闭这个流
path = contractPaths+dateTime+exportWay+"人员数据.xls";
return path;
}
对Excel内容进行编辑
private static void genExcelSocialPerson(XSSFSheet sheet, XSSFCellStyle titleStyle, XSSFCellStyle contextStyle, XSSFCellStyle contextRedColourStyle, XSSFCellStyle contextYellowColourStyle, String[][] abnormalLists1) {
//根据Excel列名长度,指定列名宽度 Excel总共10列
for (int i = 0; i < 17; i++) {
if (i == 0||i == 16) {
sheet.setColumnWidth(i, 2000);
}else if (i==5 ||i == 6||i == 9||i == 2||i ==1||i==3||i == 12||i == 13) {
sheet.setColumnWidth(i, 3000);
}else if ( i == 7 ||i == 11||i == 14||i == 15) {
sheet.setColumnWidth(i, 5000);
} else {
sheet.setColumnWidth(i, 6000);
}
}
//设置标题位置
sheet.addMergedRegion(new CellRangeAddress(
0, //first row
0, //last row
0, //first column
10 //last column
));
XSSFRow row = sheet.createRow(0);//创建第一行,为标题,index从0开始
XSSFCell cell;
cell = row.createCell(0);//创建一列
cell.setCellValue("人员信息");//标题
cell.setCellStyle(titleStyle);//设置标题样式
//注意,这种新增sheet的方式,如果你的excel原来在第一个sheet页面上有内容,会自动覆盖原来的内容,所以尽量使用空的EXCEL文档
String[] biaoti=new String[]{"序号","小区","楼幢","单元","房间号","人员类型","姓名","电话","身份证号码","户籍地","户籍地址","政治面貌","民族","学历水平","从事职业","从事单位","是否养狗"};
row = sheet.createRow(1);//创建第二行
for (int i=0;i< biaoti.length;i++){
cell = row.createCell(i);//创建第二行第一列
cell.setCellValue(biaoti[i]);//第二行第一列内容
cell.setCellStyle(contextStyle);//设置样式
}
for (int j = 0; j < abnormalLists1.length; j++) {
XSSFRow row1 = sheet.createRow(j + 2);
for (int k = 0; k < abnormalLists1[j].length; k++) {
cell = row1.createCell(k);//创建第二行第一列
cell.setCellValue(abnormalLists1[j][k]);//第二行第一列内容
cell.setCellStyle(contextStyle);//设置样式
}
}
}
下面是对Excel文本样式定义
//生成标题样式
public static XSSFCellStyle genTitleStyle(XSSFWorkbook workbook){
XSSFCellStyle style = workbook.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
style.setWrapText(true);
//标题居中,没有边框,所以这里没有设置边框,设置标题文字样式
XSSFFont titleFont = workbook.createFont();
titleFont.setBold(true);//加粗
titleFont.setFontHeight((short)15);//文字尺寸
titleFont.setFontHeightInPoints((short)15);
style.setFont(titleFont);
return style;
}
//创建文本样式
public static XSSFCellStyle genContextStyle(XSSFWorkbook workbook){
XSSFCellStyle style = workbook.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER);//文本水平居中显示
style.setVerticalAlignment(VerticalAlignment.CENTER);//文本竖直居中显示
style.setWrapText(true);//文本自动换行
//生成Excel表单,需要给文本添加边框样式和颜色
/*
CellStyle.BORDER_DOUBLE 双边线
CellStyle.BORDER_THIN 细边线
CellStyle.BORDER_MEDIUM 中等边线
CellStyle.BORDER_DASHED 虚线边线
CellStyle.BORDER_HAIR 小圆点虚线边线
CellStyle.BORDER_THICK 粗边线
*/
// style.setBorderBottom(BorderStyle.THIN);//设置文本边框
// style.setBorderLeft(BorderStyle.THIN);
// style.setBorderRight(BorderStyle.THIN);
// style.setBorderTop(BorderStyle.THIN);
return style;
}
//创建背景色红色文本样式
public static XSSFCellStyle genContextRedColourStyle(XSSFWorkbook workbook){
XSSFCellStyle style = workbook.createCellStyle();
//设置填充方案
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
//设置自定义填充颜色
style.setFillForegroundColor(new XSSFColor(new java.awt.Color(255,0,0 ), new DefaultIndexedColorMap()));
style.setAlignment(HorizontalAlignment.CENTER);//文本水平居中显示
style.setVerticalAlignment(VerticalAlignment.CENTER);//文本竖直居中显示
style.setWrapText(true);//文本自动换行
//生成Excel表单,需要给文本添加边框样式和颜色
/*
CellStyle.BORDER_DOUBLE 双边线
CellStyle.BORDER_THIN 细边线
CellStyle.BORDER_MEDIUM 中等边线
CellStyle.BORDER_DASHED 虚线边线
CellStyle.BORDER_HAIR 小圆点虚线边线
CellStyle.BORDER_THICK 粗边线
*/
// style.setBorderBottom(BorderStyle.THIN);//设置文本边框
// style.setBorderLeft(BorderStyle.THIN);
// style.setBorderRight(BorderStyle.THIN);
// style.setBorderTop(BorderStyle.THIN);
return style;
}
//创建背景色黄色文本样式
public static XSSFCellStyle genContextYellowColourStyle(XSSFWorkbook workbook){
XSSFCellStyle style = workbook.createCellStyle();
//设置填充方案
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
//设置自定义填充颜色
style.setFillForegroundColor(new XSSFColor(new java.awt.Color(255,191,0), new DefaultIndexedColorMap()));
style.setAlignment(HorizontalAlignment.CENTER);//文本水平居中显示
style.setVerticalAlignment(VerticalAlignment.CENTER);//文本竖直居中显示
style.setWrapText(true);//文本自动换行
//生成Excel表单,需要给文本添加边框样式和颜色
/*
CellStyle.BORDER_DOUBLE 双边线
CellStyle.BORDER_THIN 细边线
CellStyle.BORDER_MEDIUM 中等边线
CellStyle.BORDER_DASHED 虚线边线
CellStyle.BORDER_HAIR 小圆点虚线边线
CellStyle.BORDER_THICK 粗边线
*/
// style.setBorderBottom(BorderStyle.THIN);//设置文本边框
// style.setBorderLeft(BorderStyle.THIN);
// style.setBorderRight(BorderStyle.THIN);
// style.setBorderTop(BorderStyle.THIN);
return style;
}
最后
以上就是迷路身影为你收集整理的java生成Excel代码的全部内容,希望文章能够帮你解决java生成Excel代码所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复