Java工具类——将输出内容转换为表格形式

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import com.test.entity.Role;
import com.test.entity.User;

public class ExcelUtil {
	public static byte[] write2Excel(List<User> users){
		byte[] data=null;
		ByteArrayOutputStream out=null;
		
		try {
			//创建excel2003
			Workbook wb=new HSSFWorkbook();
			out=new ByteArrayOutputStream();
			//创建新的sheet
			Sheet sheet=wb.createSheet("allUsers");
			
			//创建第一栏(表头信息)
			Row row=sheet.createRow(0);
			String[] titleArray={
					"登录名",
					"登录类型",
					"昵称",
					"密码",
					"用户类型",
					"头像",
					"积分",
					"锁",
					"注册日期",
					"年龄",
					"性别",
					"角色"
			};
			int columnCount=titleArray.length;
			for(int i=0;i<columnCount;i++){
				Cell cell=row.createCell(i);
				cell.setCellType(HSSFCell.CELL_TYPE_STRING);
				sheet.setColumnWidth(i, 6000);
				CellStyle style=wb.createCellStyle();
				Font font=wb.createFont();
				font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
				short color=HSSFColor.RED.index;
				font.setColor(color);
				style.setFont(font);
				//填写数据
				cell.setCellStyle(style);
				cell.setCellValue(titleArray[i]);
			}
			//创建新行
			for(int j=0;j<users.size();j++){
				User user=users.get(j);
				row=sheet.createRow(j+1);
				
				row.createCell(0).setCellValue(user.getUserLoginname());
				row.createCell(1).setCellValue(user.getUserLogintype());
				row.createCell(2).setCellValue(user.getUserNickname());
				row.createCell(3).setCellValue(user.getUserPassword());
				if(user.getUserType()==0){
					row.createCell(4).setCellValue("学生");
				}else if(user.getUserType()==1){
					row.createCell(4).setCellValue("讲师");
				}else if(user.getUserType()==2){
					row.createCell(4).setCellValue("管理员");
				}else if(user.getUserType()==3){
					row.createCell(4).setCellValue("游客");
				}
				
				row.createCell(5).setCellValue(user.getUserHead());
				row.createCell(6).setCellValue(user.getUserScore());
				row.createCell(7).setCellValue(user.getUserIslock());
				row.createCell(8).setCellValue(user.getUserRegdate());
				row.createCell(9).setCellValue(user.getUserAge());
				row.createCell(10).setCellValue(user.getUserSex());
				
				if(user.getRoles()!=null && user.getRoles().size()>0){
					String roleName="";
					for(Role role : user.getRoles()){
						roleName+=role.getName()+",";
					}
					row.createCell(11).setCellValue(roleName.substring(0,roleName.length()-1));
				
				}else{
					row.createCell(11).setCellValue("无角色");
				}
			}
			wb.write(out);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(out!=null){
				try {
					out.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		data=out.toByteArray();
		return data;
	}
}
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值