使用的jxl.jar包680kb左右的大小
示例代码:
<pre name="code" class="java"></pre><p><pre name="code" class="java"> class JXLtest {
public void makeExcel(){
// 创建文件夹
String filePath ="D:/memo.Excel";
try {
File dir = new File(filePath);
if (!dir.exists()) {
dir.mkdirs();
}
WritableWorkbook workBook= Workbook.createWorkbook(dir);
WritableSheet workSheet = workBook.createSheet("周排行", 0); //sheet页名称。
workSheet.setColumnView(0, 20); // 设置列的宽度
workSheet.setColumnView(0, 20); // 设置列的宽度
workSheet.setColumnView(0, 20); // 设置列的宽度
workSheet.setRowView(0, 40);// 设置行高
WritableFont fontStyle = new WritableFont(WritableFont.ARIAL, 11, WritableFont.NO_BOLD,false); //cell字体
DisplayFormat displayFormat = NumberFormats.TEXT;
WritableCellFormat tableStyle =new WritableCellFormat(fontStyle,displayFormat); //建立cell格样式对象 加入字体样式。
tableStyle.setBackground(jxl.format.Colour.BLACK); //cell格背景色
tableStyle.setAlignment(jxl.format.Alignment.JUSTIFY); //cell格左右对其方式
tableStyle.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);//cell格上下对其方式
tableStyle.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN); //cell格边框
jxl.write.Number number = new jxl.write.Number(0, 0,Double.parseDouble("1"),tableStyle);
workSheet.addCell(number);//数值格式的cell
workSheet.addCell(new Label(1, 0, "ceshi", tableStyle));//文本格式的cell
File imgFile = new File("D:/test.png"); //图片插入
// 开始位置
double picBeginCol = Integer.valueOf(1).intValue();
double picBeginRow = Integer.valueOf(2).intValue();
// 图片时间的高度,宽度
double picCellWidth = 0.0;
double picCellHeight = 0.0;
// 读入图片
BufferedImage picImage = ImageIO.read(imgFile);
// 取得图片的像素高度,宽度
int picWidth = picImage.getWidth();
int picHeight = picImage.getHeight();
//生成一个图片对象。
WritableImage image = new WritableImage(picBeginCol, picBeginRow, picCellWidth, picCellHeight, imgFile);
// 把图片插入到sheet
workSheet.addImage(image);
workBook.write();// 写入Exel工作表
workBook.close();// 关闭Excel工作薄对象
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RowsExceededException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}