package com.bmw.ntt.utils;
import jxl.Workbook;
import jxl.format.ScriptStyle;
import jxl.format.UnderlineStyle;
import jxl.read.biff.BiffException;
import jxl.write.*;
import jxl.write.biff.RowsExceededException;
import java.io.File;
import java.io.IOException;
public class POIUtils {
private final static String templateUrl = "D:\\SvnWorkSpace\\运维项目\\01 Training Academy\\02_TTM\\01_工作区\\201712001CR\\01_工程区\\03_配置指导书\\01_配置开发\\CR2018001\\resources\\template\\ExamNoticeTemplate.xls";
public static void main(String[] args) {
try {
//WritableWorkbook workbook = Workbook.createWorkbook(new File("d:\\test.xls"));
Workbook wb = Workbook.getWorkbook(new File(templateUrl)); // 获得原始文档
WritableWorkbook workbook = Workbook.createWorkbook(new File("d:\\test_modified.xls"), wb); // 创建一个可读写的副本
WritableSheet sheet = workbook.getSheet(0);
sheet.setName("修改后"); // 给sheet页改名
workbook.removeSheet(2); // 移除多余的标签页
workbook.removeSheet(3);
sheet.mergeCells(0, 0, 4, 0); // 合并单元格
sheet.setRowView(0, 600); // 设置行的高度
sheet.setColumnView(0, 30); // 设置列的宽度
sheet.setColumnView(1, 20); // 设置列的宽度
WritableCell cell = sheet.getWritableCell(2,3);
// 通过WritableFont、WritableCellFormat等对象可以设置单元格的字体、样式等外观:
WritableFont titleWf = new WritableFont(WritableFont.createFont("仿宋_GB2312"),// 字体
20,//WritableFont.DEFAULT_POINT_SIZE, // 字号
WritableFont.NO_BOLD, // 粗体
false, // 斜体
UnderlineStyle.NO_UNDERLINE, // 下划线
Colour.BLUE2, // 字体颜色
ScriptStyle.NORMAL_SCRIPT);
WritableCellFormat wcf = new WritableCellFormat(titleWf);
wcf.setBackground(Colour.GRAY_25);// 设置单元格的背景颜色
wcf.setAlignment(Alignment.CENTRE); // 设置对齐方式
wcf.setBorder(Border.ALL, BorderLineStyle.THICK); // 添加边框
cell.setCellFormat(wcf);
//在jxl中,有几种常用的数据类型,根据单元格内数据类型的不同,每个WritableCell可以根据其类型被转换为它的一个子类型,以便对不同类型的数据进行专门的处理,通常可以做这样的转换:
WritableCell fromCell = sheet.getWritableCell(0, 1);
if (fromCell instanceof jxl.write.Number) {
jxl.write.Number num = (jxl.write.Number) fromCell;
} else if (fromCell instanceof jxl.write.Boolean) {
jxl.write.Boolean bool = (jxl.write.Boolean) fromCell;
} else if (fromCell instanceof jxl.write.DateTime) {
jxl.write.DateTime dt = (jxl.write.DateTime) fromCell;
} else if(fromCell instanceof Label){
Label _label = (Label) fromCell;
}
} catch (IOException e) {
e.printStackTrace();
} catch (BiffException e) {
e.printStackTrace();
} catch (RowsExceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
}
// /**
// * 把一个excel中的cellstyletable复制到另一个excel,这里会报错,不能用这种方法,不明白呀?????
// * @param fromBook
// * @param toBook
// */
// public static void copyBookCellStyle(HSSFWorkbook fromBook,HSSFWorkbook toBook){
// for(short i=0;i<fromBook.getNumCellStyles();i++){
// HSSFCellStyle fromStyle=fromBook.getCellStyleAt(i);
// HSSFCellStyle toStyle=toBook.getCellStyleAt(i);
// if(toStyle==null){
// toStyle=toBook.createCellStyle();
// }
// copyCellStyle(fromStyle,toStyle);
// }
// }
}