使用 jxl.jar 架包生成 Excel文件

package com.util;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.esri.arcgis.display.NewTextBezierCurveFeedback;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.format.Pattern;
import jxl.write.Label;
import jxl.write.VerticalAlignment;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.biff.RowsExceededException;

@SuppressWarnings({ "unused", "deprecation" })
public class WorkBookFactory {
	/**
	 * 获取一个工作薄实例
	 */
	
	@SuppressWarnings({ "unused", "rawtypes" })
	public static  WritableWorkbook getWritableWorkbookInstance(OutputStream outputstream,String sheetTile,int columnCount,List list){
		WritableWorkbook workBook = null;
		try {
			
			  workBook = Workbook.createWorkbook(outputstream);
			  WritableSheet sheet = workBook.createSheet(sheetTile, 0);
				//设置列宽
				for(int i= 0;i<columnCount;i++)
				{
					sheet.setRowView(0, (short)(1.7*256)); // 设置行的高度  
					sheet.setColumnView(i, 20); // 默认列的宽度  
					sheet.setColumnView(0, 30); // 设置列的宽度  
				}
				WritableFont titlefontStyle = new WritableFont(WritableFont.createFont("微软雅黑"), 14);   
				titlefontStyle.setBoldStyle(WritableFont.BOLD);//设置字体加粗
				WritableCellFormat titleFormate = new WritableCellFormat(titlefontStyle);//生成一个单元格样式控制对象
//				titleFormate.setBackground(Colour.BLUE_GREY, Pattern.SOLID);
//				titleFormate.setBackground(Colour.DARK_BLUE2, Pattern.SOLID);//这个深蓝底
//				titleFormate.setBackground(Colour.OCEAN_BLUE, Pattern.SOLID);//海蓝底背景
				titleFormate.setBackground(Colour.DARK_TEAL, Pattern.SOLID);//这个黑蓝底
				titleFormate.setAlignment(Alignment.CENTRE); // 设置为右对齐  
				titleFormate.setVerticalAlignment(VerticalAlignment.CENTRE);//设置垂直居中
				titleFormate.setBorder(Border.ALL, BorderLineStyle.HAIR); // 添加边框  
				titlefontStyle.setColour(Colour.WHITE);
				String[]  title = {"时间","时间","时间","时间","时间","时间","时间"};
				for(int i= 0 ;i<columnCount;i++)
				{
					Label label = new Label(i,0,title[i],titleFormate);//参数含义 Label(column,row,content,style)
					sheet.addCell(label);
				}
				//*********************以下为内容样式****************************
				 WritableFont fontStyle = new WritableFont(WritableFont.createFont("宋体"), 12);   
		         fontStyle.setColour(Colour.BLACK);//字体颜色
		         WritableCellFormat cellFormat = new WritableCellFormat(fontStyle);  
		         cellFormat.setBackground(Colour.WHITE, Pattern.SOLID);// 设置单元格的背景颜色  
//		         cellFormat.setAlignment(Alignment.JUSTIFY); //自动调整 单元格内容,设置这个跟设置左右对齐会冲突,只能应用其中一个样式
		         cellFormat.setAlignment(Alignment.CENTRE); // 设置为右对齐  
		         cellFormat.setVerticalAlignment(VerticalAlignment.CENTRE);//设置垂直居中
		         //设置边框xian
//		         cellFormat.setBorder(Border.ALL, BorderLineStyle.MEDIUM); // 中等细黑边框
		         cellFormat.setBorder(Border.ALL, BorderLineStyle.getStyle(1)); // 这个方法可以设置边框线条粗细
		         //填充数据进入  Excel
		         for(int i = 0 ;i <list.size();i++){
		        	 sheet.setRowView(i+1, (short)(1.6*256)); // 设置行的高度  
		        	 Map map = (Map)list.get(i);
		        	 Object[] objects={map.get("a"),map.get("b"),map.get("c"),map.get("d")};
		        	 for(int j= 0;j<columnCount;j++)
		        	 {
		        		 Label label = new Label(j,(i+1),objects[j].toString(),cellFormat);//Label(column,row,content,style)
		        		 sheet.addCell(label);
		        	 }
		         }
		} catch (Exception e) {
			e.printStackTrace();
		} 
		
		return workBook;
	}
	  
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值