所需jar包
String file = "excel01.xlsx";
try {
// 创建excel文件,并简单写入数据
// writeExcel(file);
// 创建excel文件,并设置单元格格式
// excelCellType(file);
// 设置excel单元格样式
// excelStyle(file);
// 单元格合并
exceMerge(file);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/**
* 创建excel文件,并写入数据
* @param path
* @throws IOException
*/
public static void writeExcel(String path) throws IOException {
// System.getProperty("user.dir") 获取项目根路径
String filePath = System.getProperty("user.dir") + File.separator + "src/cn/kk/excel/file/" + path;
System.out.println(filePath);
File file = new File(filePath);
// 判断文件是否存在
if (!file.exists()) {
// 创建文件
file.createNewFile();
}
Workbook workBook = null;
// 根据文件名称后缀,创建excel模板
if (file.getName().endsWith(".xls")) {
workBook = new HSSFWorkbook();//.xls后缀模板
}else if (file.getName().endsWith(".xlsx")) {
workBook = new XSSFWorkbook();//.xlsx后缀模板
}
// 创建工作薄
Sheet sheet = workBook.createSheet("sheet1");
Sheet sheet1 = workBook.createSheet("sheet2");
// 创建行,下标从0开始
Row row = sheet.createRow(0);
// 创建列,下标从0开始
Cell cell = row.createCell(0);
// 给单元格设值
cell.setCellValue("hello");
// 把excel写入到文件中
OutputStream out = FileUtils.openOutputStream(file);
workBook.write(out);
out.close();
}
/**
* 创建excel文件,并设置单元格格式
* @param path
* @throws IOException
*/
public static void excelCellType(String path) throws IOException {
// System.getProperty("user.dir") 获取项目根路径
String filePath = System.getProperty("user.dir") + File.separator + "src/cn/kk/excel/file/" + path;
System.out.println(filePath);
File file = new File(filePath);
// 判断文件是否存在
if (!file.exists()) {
// 创建文件
file.createNewFile();
}
Workbook workBook = null;
// 根据文件名称后缀,创建excel模板
if (file.getName().endsWith(".xls")) {
workBook = new HSSFWorkbook();//.xls后缀模板
}else if (file.getName().endsWith(".xlsx")) {
workBook = new XSSFWorkbook();//.xlsx后缀模板
}
// 创建工作薄
Sheet sheet = workBook.createSheet("sheet1");
// 创建行,下标从0开始
Row row0 = sheet.createRow(0);
// 创建列,下标从0开始
Cell cell01 = row0.createCell(0);
Cell cell02 = row0.createCell(1);
Cell cell03 = row0.createCell(2);
Cell cell04 = row0.createCell(3);
// 给单元格设值 表头
cell01.setCellValue("常规");
cell02.setCellValue("数值");
cell03.setCellValue("日期");
cell04.setCellValue("下拉框");
Row row1 = sheet.createRow(1);
// 默认单元格常规格式
Cell cell11 = row1.createCell(0);
// 数值格式
Cell cell12 = row1.createCell(1);
// 设置单元格格式
DataFormat dataFormat = workBook.createDataFormat();
//设置标题单元格类型
CellStyle cellStyleTitle = workBook.createCellStyle();
cellStyleTitle.setDataFormat(dataFormat.getFormat("0.0"));//设置值格式,0不保留小数。 0.0保留一位小数。0.00保留两位小数
cell12.setCellType(HSSFCell.CELL_TYPE_NUMERIC);//设置单元格格式为数字格式
cell12.setCellStyle(cellStyleTitle);
// 日期格式
Cell cell13 = row1.createCell(2);
CellStyle cellStyleTitle1 = workBook.createCellStyle();
cell13.setCellType(HSSFCell.CELL_TYPE_NUMERIC);//设置单元格格式为日期格式
cellStyleTitle1.setDataFormat(dataFormat.getFormat("yyyy-mm-dd"));
cell13.setCellStyle(cellStyleTitle1);
// 下拉框
Cell cell14 = row1.createCell(3);
cell14.setCellValue("请选择");
String[] selectVals = {"请选择","男","女"};
//
DataValidationHelper helper = sheet.getDataValidationHelper();
// 设置下拉框选项的值
DataValidationConstraint constraint = helper.createExplicitListConstraint(selectVals);
CellRangeAddressList addressList = new CellRangeAddressList(1, 1, 3, 3);
DataValidation dataValidation = helper.createValidation(constraint, addressList);
// .xls后缀不用设置
if (file.getName().endsWith(".xlsx")) {
// 设置开启错误提示
dataValidation.setShowErrorBox(true);
// 设置提示信息
dataValidation.setShowPromptBox(true);
}
dataValidation.createErrorBox("错误提示","请从下拉框中选择");
dataValidation.createPromptBox("提示","输入时提示信息");
sheet.addValidationData(dataValidation);
// 把excel写入到文件中
OutputStream out = FileUtils.openOutputStream(file);
workBook.write(out);
out.close();
}
/**
* 设置excel单元格样式
* @param path
* @throws IOException
*/
public static void excelStyle(String path) throws IOException {
// System.getProperty("user.dir") 获取项目根路径
String filePath = System.getProperty("user.dir") + File.separator + "src/cn/kk/excel/file/" + path;
System.out.println(filePath);
File file = new File(filePath);
// 判断文件是否存在
if (!file.exists()) {
// 创建文件
file.createNewFile();
}
Workbook workBook = null;
// 根据文件名称后缀,创建excel模板
if (file.getName().endsWith(".xls")) {
workBook = new HSSFWorkbook();//.xls后缀模板
}else if (file.getName().endsWith(".xlsx")) {
workBook = new XSSFWorkbook();//.xlsx后缀模板
}
// 创建工作薄
Sheet sheet = workBook.createSheet("sheet1");
// 设置行宽度
sheet.setColumnWidth(0, 50*256);//实际宽度为49.22
// 创建行,下标从0开始
Row row0 = sheet.createRow(0);
// 创建列,下标从0开始
Cell cell01 = row0.createCell(0);
Cell cell02 = row0.createCell(1);
Cell cell03 = row0.createCell(2);
Cell cell04 = row0.createCell(3);
// 给单元格设值 表头
cell01.setCellValue("常规111111111111111");
cell02.setCellValue("数值");
cell03.setCellValue("日期");
cell04.setCellValue("下拉框");
//设置标题单元格类型
CellStyle cellStyleTitle = workBook.createCellStyle();
cellStyleTitle.setFillForegroundColor(IndexedColors.LIME.getIndex());//设置Excel单元格背景色
cellStyleTitle.setFillPattern(CellStyle.SOLID_FOREGROUND);//设置图案样式
cellStyleTitle.setAlignment(CellStyle.ALIGN_CENTER); //水平布局:居中
cellStyleTitle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);//垂直居中
// cellStyleTitle.setWrapText(true);//设置自动换行
//设置单元格类型
Font fontTitle = workBook.createFont();
fontTitle.setFontName("宋体"); //字体
fontTitle.setFontHeightInPoints((short) 18); //字体大小
fontTitle.setColor(HSSFColor.BLACK.index); //字体颜色
fontTitle.setBoldweight(Font.BOLDWEIGHT_BOLD); //粗体显示
//font.setItalic(true); //是否使用斜体
//font.setStrikeout(true); //是否使用划线
cellStyleTitle.setFont(fontTitle);
cell01.setCellStyle(cellStyleTitle);
// 把excel写入到文件中
OutputStream out = FileUtils.openOutputStream(file);
workBook.write(out);
out.close();
}
/**
* 单元格合并
* @param path
* @throws IOException
*/
public static void exceMerge(String path) throws IOException {
// System.getProperty("user.dir") 获取项目根路径
String filePath = System.getProperty("user.dir") + File.separator + "src/cn/kk/excel/file/" + path;
System.out.println(filePath);
File file = new File(filePath);
// 判断文件是否存在
if (!file.exists()) {
// 创建文件
file.createNewFile();
}
Workbook workBook = null;
// 根据文件名称后缀,创建excel模板
if (file.getName().endsWith(".xls")) {
workBook = new HSSFWorkbook();//.xls后缀模板
}else if (file.getName().endsWith(".xlsx")) {
workBook = new XSSFWorkbook();//.xlsx后缀模板
}
// 创建工作薄
Sheet sheet = workBook.createSheet("sheet1");
// 设置行宽度
sheet.setColumnWidth(0, 50*256);//实际宽度为49.22
// 创建行,下标从0开始
Row row0 = sheet.createRow(0);
// 创建列,下标从0开始
Cell cell01 = row0.createCell(0);
// 给单元格设值 表头
cell01.setCellValue("列合并,第一行4列合并");
// 合并单元格
// new CellRangeAddress(firstRow, lastRow, firstCol, lastCol)
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 3));
Row row1 = sheet.createRow(1);
Cell cell11 = row1.createCell(0);
cell11.setCellValue("行合并,第一列,第二行开始合并3行");
sheet.addMergedRegion(new CellRangeAddress(1, 3, 0, 0));
// 把excel写入到文件中
OutputStream out = FileUtils.openOutputStream(file);
workBook.write(out);
out.close();
}