importjava.io.File;importjava.io.IOException;importjava.util.Map;importjava.util.Set;importjxl.Workbook;importjxl.read.biff.BiffException;importjxl.write.WritableSheet;importjxl.write.WritableWorkbook;importjxl.write.WriteException;/*** 数据写入Excel工具类
*
*@author姓名 工号
*@version[版本号, 2019年6月10日]
*@see[相关类/方法]
*@since[产品/模块版本]*/
public final classJxlExcelWriter
{//可写的工作薄
privateWritableWorkbook workbook;//只读工作薄
privateWorkbook book;/*** 构造方法
*
*@paramexcel Excel文件对象,允许其文件本身不存在
* 若excel文件不存在,构造方法将自动创建一个文件对象对应的磁盘文件*/
publicJxlExcelWriter(File excel)
{try{//Excel文件不存在时,初始化一个可写入的工作薄
if (!excel.exists())
{
File file=prepareFile(excel);
workbook=Workbook.createWorkbook(file);
}/*** Excel文件存在时,表明Excel中至少有一个工作表,
* 初始化一个可向工作表追加数据且能写入新数据的工作薄*/
else{
book=Workbook.getWorkbook(excel);/*** 此静态方法通过传入两个参数,Excel文件对象excel、只读工作工作薄对象book,
* 来创建初始化一个可追加数据的工作薄对象*/workbook=Workbook.createWorkbook(excel, book);
}
}catch (IOException |BiffException e)
{
e.printStackTrace();
}
}/*** 准备Excel文件
*
*@paramfile
*@return*@see[类、类#方法、类#成员]*/
privateFile prepareFile(File file)
{/** file对象对应的excel文件不存在, 先创建其上级目录,再创建excel文件本身。*/File parentfile=file.getParentFile();
parentfile.mkdirs();
file= newFile(parentfile, file.getName());try{
file.createNewFile();
}catch(IOException e)
{
e.printStackTrace();
}returnfile;
}/*** 将多行数据写入到Excel缓冲区
*
*@paramcontents 要写入到Excel的数据(映射表),其key值表示行索引,
* 其value值表示一行所有的单元格内容,字符串数组的每个元素对应一个单元格内容
*@paramsheetIndex 要写入到Excel的工作表索引
*@see[类、类#方法、类#成员]*/
public void write(Map contents, intsheetIndex)
{
String sheetName=generateSheetName(sheetIndex);
write(contents, sheetName, sheetIndex);
}/*** 将多行数据写入到Excel缓冲区
*
*@paramcontents 要写入到Excel的数据(映射表),其key值表示行索引,
* 其value值表示一行所有的单元格内容,字符串数组的每个元素对应一个单元格内容
*@paramsheetName 要写入到Excel的工作表名
*@paramsheetIndex 要写入到Excel的工作表索引*/
public void write(Map contents, String sheetName, intsheetIndex)
{if (contents == null ||contents.isEmpty())throw new IllegalArgumentException("参数contents不包含任何内容或为空指针");//得到工作表
WritableSheet sheet =getWritableSheet(sheetName, sheetIndex);//将数据添加到工作表的缓冲区中
try{
Set keys =contents.keySet();for (introwIndex : keys)
{
String[] rowContent=contents.get(rowIndex);for (int i = 0; i < rowContent.length; i++)
{//文本内容为空时,sheet表增加一个Blank
if (rowContent[i] == null)
{
jxl.write.Blank blank= newjxl.write.Blank(i, rowIndex);
sheet.addCell(blank);
}else{
jxl.write.Label lable= newjxl.write.Label(i, rowIndex, rowContent[i]);
sheet.addCell(lable);
}
}
}
}catch(WriteException e)
{
e.printStackTrace();
}
}/*** 创建/获取工作表
*
*@paramsheetName
*@paramsheetIndex
*@return*@see[类、类#方法、类#成员]*/
private WritableSheet getWritableSheet(String sheetName, intsheetIndex)
{
WritableSheet sheet= null;if (sheetIndex 
{
sheet=workbook.getSheet(sheetIndex);
sheet.setName(sheetName);
}else{
sheet=workbook.createSheet(sheetName, sheetIndex);
}returnsheet;
}/*** 生成工作表表名
*
*@paramsheetIndex
*@return*@see[类、类#方法、类#成员]*/
private String generateSheetName(intsheetIndex)
{
String sheetName= "";if (sheetIndex 
{
sheetName=workbook.getSheet(sheetIndex).getName();
}else{
sheetName= "sheet" +sheetIndex;
}returnsheetName;
}/*** 将单行数据写入到Excel缓冲区
*
*@paramrowContent 要写入到Excel的数据,廖数组的每个元素对应一个单元格内容
*@paramrowIndex 写入到Excel的行索引
*@paramsheetIndex 要写入到Excel的工作表索引
*@see[类、类#方法、类#成员]*/
public void writeRow(String[] rowContent, int rowIndex, intsheetIndex)
{
String sheetName=generateSheetName(sheetIndex);
writeRow(rowContent, rowIndex, sheetName, sheetIndex);
}/*** 将单行数据写入到Excel缓冲区
*
*@paramrowContent 要写入到Excel的数据,廖数组的每个元素对应一个单元格内容
*@paramrowIndex 写入到Excel的行索引
*@paramsheetName 要写入到Excel的工作表名
*@paramsheetIndex 要写入到Excel的工作表索引
*@see[类、类#方法、类#成员]*/
public void writeRow(String[] rowContent, int rowIndex, String sheetName, intsheetIndex)
{//得到工作表
WritableSheet sheet =getWritableSheet(sheetName, sheetIndex);try{for (int i = 0; i < rowContent.length; i++)
{if (rowContent[i] == null)
{
jxl.write.Blank blank= newjxl.write.Blank(i, rowIndex);
sheet.addCell(blank);
}else{
jxl.write.Label lable= newjxl.write.Label(i, rowIndex, rowContent[i]);
sheet.addCell(lable);
}
}
}catch(WriteException e)
{
e.printStackTrace();
}
}/*** 写入一个单元内容到Excel缓冲区
*
*@paramcontent 要写入的单元格内容
*@paramrowIndex 写入到Excel的行索引
*@paramcolIndex 写入到Excel的列索引
*@paramsheetIndex 要写入到Excel的工作表索引*/
public void writeCell(String content, int rowIndex, int colIndex, intsheetIndex)
{
String sheetName=generateSheetName(sheetIndex);
writeCell(content, rowIndex, colIndex, sheetName, sheetIndex);
}/*** 写入一个单元内容到Excel缓冲区
*
*@paramcontent 要写入的单元格内容
*@paramrowIndex 写入到Excel的行索引
*@paramcolIndex 写入到Excel的列索引
*@paramsheetName 要写入到Excel的工作表名
*@paramsheetIndex 要写入到Excel的工作表索引*/
public void writeCell(String content, int rowIndex, int colIndex, String sheetName, intsheetIndex)
{//得到工作表
WritableSheet sheet =getWritableSheet(sheetName, sheetIndex);try{if (content == null)
{
jxl.write.Blank blank= newjxl.write.Blank(colIndex, rowIndex);
sheet.addCell(blank);
}else{
jxl.write.Label lable= newjxl.write.Label(colIndex, rowIndex, content);
sheet.addCell(lable);
}
}catch(WriteException e)
{
e.printStackTrace();
}
}/*** 将Excel缓冲区的数据刷新写入到Excel文件中 
* 在最后此方法必须被调用,否则数据不能真正写入Excel文件中*/
public voidflush()
{try{if (workbook != null)
{
workbook.write();
workbook.close();
}if (book != null)
{
book.close();
}
}catch (WriteException |IOException e)
{
e.printStackTrace();
}
}
}