标签:bsp param ram ble 选择 导出excel free span inpu
/**
* 导入Excel文件数据
*
* @param file 将要导入的Excel文件
* @param fileCheckKeyWord 用于判断导入文件是否正确的关键字符串
* @param keyX 用于判断导入文件是否正确的关键字符串所在的x坐标位置
* @param keyY 用于判断导入文件是否正确的关键字符串所在的y坐标位置
* @param startRow 导入数据开始行数
* @param startLine 导入数据开始列数
*
* @return List《Object[]》
* @throws Exception
*/
public static List<Object[]> excelImport(File file, String fileCheckKeyWord, int keyX, int keyY, int startRow, int startLine) throws Exception{
List<Object[]> dataList = new ArrayList<Object[]>();
InputStream inputStream = new FileInputStream(file);
WorkbookSettings wbs = new WorkbookSettings();
wbs.setSuppressWarnings(true);
Workbook book = Workbook.getWorkbook(inputStream, wbs);
Sheet sh = book.getSheet(0);
//判断选择excel文件是否正确
String typeContent = sh.getCell(keyX, keyY).getContents().trim();
if(!typeContent.equals(fileCheckKeyWord)){
return dataList;
}
//行数
int rows = sh.getRows();
if(rows >= 65536){//超出 excel 03最大行数
return dataList;
}
//列数
int lines = sh.getColumns();
//判断导入的数据是否有误
Object[] dataObjArr = null;
for(int i = startRow; i <= rows; i++){
dataObjArr = new Object[lines - startLine + 1];
for(int j = startLine; j <= lines; j++){
String content = sh.getCell(j, i).getContents().trim();
if(ComFun.strNull(content) && !content.trim().equals("")){
dataObjArr[j - startLine] = content;
} else {
dataObjArr[j - startLine] = "";
}
}
}
return dataList;
}
/**
* 导出EXCEL文件
*
* @param fileName 导出文件名 (为NULL时,文件名称按照时间戳生成)
* @param headCellNames 头部标题名称组
* @param dataList 数据List,与参数headCellNames对应
*/
public static void excelExport(String fileName,
String[] headCellNames, List<Object[]> dataList) throws IOException, WriteException {
if(ComFun.strNull(dataList) && dataList.size() > 0){
if(!ComFun.strNull(fileName)){
fileName = DateFormatUtil.dateToStr(new Date(), DateFormatUtil.TYPE4);
}
String[] cellNames = new String[headCellNames.length + 1];
cellNames[0] = "序号";
for(int i=0; i<headCellNames.length; i++){
cellNames[i+1] = headCellNames[i];
}
WritableWorkbook book = Workbook.createWorkbook(new File("C:\\ACjba\\fileDownload\\" + fileName + ".xls"));// 创建文件
WritableSheet sheet = book.createSheet(fileName, 0);// 创建工作薄
sheet.setName(fileName);
sheet.getSettings().setVerticalFreeze(2);// 冻结前两行
/* 开始创建sheet第一行 */
// 标题字体、样式
WritableFont titleFont = new WritableFont(WritableFont.ARIAL, 12,
WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
Colour.BLACK);// 设置标题字体
WritableCellFormat titleFormat = new WritableCellFormat(titleFont);// 设置标题格式
titleFormat.setAlignment(Alignment.CENTRE);
titleFormat.setVerticalAlignment(VerticalAlignment.CENTRE);
// 合并单元格
sheet.mergeCells(1, 0, cellNames.length, 0);
// 设置行高
sheet.setRowView(0, 800, false);// 40磅
// label理解为sheet中的单元格,此处在第1列0行加入指定内容、样式
Label titleLabel = new Label(1, 0, fileName, titleFormat);
// 将label加入到sheet中,第一行创建完成
sheet.addCell(titleLabel);
/* 开始创建第二行 */
// 表头字体、样式
WritableFont cellFont = new WritableFont(WritableFont.ARIAL, 9,
WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,
Colour.BLACK);
WritableCellFormat cellFormat = new WritableCellFormat(cellFont);
// 设置自动换行
cellFormat.setWrap(true);
cellFormat.setAlignment(Alignment.CENTRE);
cellFormat.setVerticalAlignment(VerticalAlignment.CENTRE);
cellFormat.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);// 设置边框
// 设置列宽
for (int i = 1; i <= cellNames.length; i++) {
sheet.setColumnView(i, 20);
sheet.setRowView(1, 440);// 22磅
sheet.addCell(new Label(i, 1, cellNames[i - 1], cellFormat));
}
// 创建第三行
sheet.setRowView(2, 440);// 22磅
/* 循环创建传入集合(第四行以后) */
WritableFont bodyFont = new WritableFont(WritableFont.ARIAL, 9,
WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,
Colour.BLACK);
WritableCellFormat bodyFormat = new WritableCellFormat(bodyFont);
// 设置自动换行
bodyFormat.setWrap(true);
bodyFormat.setAlignment(Alignment.CENTRE);
bodyFormat.setVerticalAlignment(VerticalAlignment.CENTRE);
bodyFormat.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);// 设置边框
int r = 2;
for (int i=0;i<dataList.size();i++) {
sheet.setRowView(r, 440);
sheet.addCell(new Label(1, r, String.valueOf(r - 1), bodyFormat));// 序号
for(int j=2; j<cellNames.length+1; j++){
sheet.addCell(new Label(j, r, String.valueOf(dataList.get(i)[j-2]), bodyFormat));
}
r++;
}
book.write();
book.close();
}
}
标签:bsp param ram ble 选择 导出excel free span inpu
原文地址:https://www.cnblogs.com/3b2414/p/9323260.html