java操作Excel之二:jxl生成excel

使用的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();
					}
    				
        		}
 
	}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值