JAVA导出EXCEL格式

controller层

    @RequestMapping("/excelModify")
    @ResponseBody
    public void  excelModify(@RequestParam("YearMonth")String YearMonth, HttpServletResponse response) throws IOException {
        //生成一个列表信息
        Map<String, Object> cond = new HashMap<String, Object>();
        cond.put("querytime",YearMonth);
        List<Achievements> list = achievementsService.achievementselect(cond);
        HSSFWorkbook wb = achievementsService.export(list,YearMonth);
        response.setContentType("multipart/form-data");
        response.setCharacterEncoding("utf-8");
        String name = YearMonth.split("-")[0]+"年"+YearMonth.split("-")[1]+"月"+"信息技术部绩效积分统计";
        response.setHeader("Content-disposition", "attachment;filename=" + new String( name.getBytes("gb2312"), "ISO8859-1" ) + ".xlsx");
        OutputStream ouputStream = response.getOutputStream();
        wb.write(ouputStream);
        ouputStream.flush();
        ouputStream.close();

    }

service层

public HSSFWorkbook export(List<Achievements> list,String YearMonth) {
            //定义表头
            HSSFWorkbook wb = new HSSFWorkbook();
            //生成一个工作表

            HSSFSheet sheet = wb.createSheet("信息技术部绩效积分统计");

            sheet.setColumnWidth(0, 2000);
            sheet.setColumnWidth(1, 3766);
            sheet.setColumnWidth(2, 3000);
            sheet.setColumnWidth(3, 3766);
            sheet.setColumnWidth(4, 3000);
            sheet.setColumnWidth(5, 3766);
            sheet.setColumnWidth(6, 3766);
            sheet.setColumnWidth(7, 2766);
            sheet.setColumnWidth(8, 1600);
            sheet.setColumnWidth(9, 1200);
            sheet.setColumnWidth(10, 1200);
            sheet.setColumnWidth(11, 2766);
            sheet.setColumnWidth(12, 3066);
            sheet.setColumnWidth(13, 3566);
            sheet.setColumnWidth(14, 3066);
            sheet.setColumnWidth(15, 2066);
            sheet.setColumnWidth(16, 1766);
            sheet.setColumnWidth(17, 3266);


            CellRangeAddress cra = new CellRangeAddress(0,0 , 0, 17);
            sheet.addMergedRegion(cra);
            //生成第一行
            HSSFRow row = sheet.createRow(0);
            //生成单元格的样式style
            HSSFCellStyle style = wb.createCellStyle();
            HSSFFont redFont = wb.createFont();
            redFont.setFontHeightInPoints((short)20);
            redFont.setBold(true);
            style.setFont(redFont);
            style.setAlignment(HorizontalAlignment.CENTER);
            style.setVerticalAlignment(VerticalAlignment.CENTER);
            row.setRowStyle(style);
            row.setHeightInPoints(30);

            HSSFCellStyle style1 = wb.createCellStyle();
            HSSFFont redFont1 = wb.createFont();
            redFont1.setBold(true);
            redFont1.setFontHeightInPoints((short)11);
            style1.setFont(redFont1);
            style1.setAlignment(HorizontalAlignment.CENTER);
            style1.setVerticalAlignment(VerticalAlignment.CENTER);

            HSSFCellStyle style2 = wb.createCellStyle();
            HSSFFont redFont2 = wb.createFont();
            //字体大小
            redFont2.setFontHeightInPoints((short)10);
            style2.setFont(redFont2);
            //自动换行
            style2.setWrapText(true);
            //水平
            style2.setAlignment(HorizontalAlignment.CENTER);
            //垂直
            style2.setVerticalAlignment(VerticalAlignment.CENTER);

            HSSFCellStyle style3 = wb.createCellStyle();
            HSSFFont redFont3 = wb.createFont();
            redFont3.setFontHeightInPoints((short)10);
            style3.setFont(redFont3);
            style3.setWrapText(true);
            style3.setAlignment(HorizontalAlignment.LEFT);
            style3.setVerticalAlignment(VerticalAlignment.TOP);



            Cell cell = row.createCell(0);
            cell.setCellValue(YearMonth.split("-")[0]+"年"+YearMonth.split("-")[1]+"月"+"信息技术部绩效积分统计");
            cell.setCellStyle(style);

            String[] titles = {"姓名","开发进度考核","进度权重","开发质量考核","质量权重","质量合格率","日常维护考核","维护权重","综合","奖","罚","工资计分","应上班天数","实际上班天数",
            "岗位工资","绩效奖","级别","月考核等级"};
            row = sheet.createRow(row.getRowNum() + 1);
            for(int i=0;i<titles.length;i++){
                Cell cell1 = row.createCell(i);
                cell1.setCellValue(titles[i]);
                cell1.setCellStyle(style1);
            }
            row.setHeightInPoints(20);

            String remarks = "";
            String createname = "";
            String createtime = "";
            String examine= "";
            String examinetime= "";
            String approval= "";
            String approvaltime= "";

            SimpleDateFormat sdf1=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");


            for (int i = 0; i < list.size(); i++) {
                //得到当前行数的下一行(row.getRowNum():得到当前行数)
                row = sheet.createRow(row.getRowNum() + 1);
                Achievements achievements = list.get(i);
                //赋值
                row.setRowStyle(style2);
                row.createCell(0).setCellValue(achievements.getUsername());
                row.getCell(0).setCellStyle(style2);

                remarks = achievements.getRemarks();
                createname = achievements.getCreatename();
                createtime = sdf1.format(achievements.getCreatetime());
                approval = achievements.getApproval();
                approvaltime = sdf1.format(achievements.getApprovaltime());

                if(achievements.getProgress()==null){
                    row.createCell(1).setCellValue("");
                    row.getCell(1).setCellStyle(style2);
                }else {
                    row.createCell(1).setCellValue(achievements.getProgress());
                    row.getCell(1).setCellStyle(style2);
                }

                if(achievements.getWeight()==null){
                    row.createCell(2).setCellValue("");
                    row.getCell(2).setCellStyle(style2);
                }else {
                    row.createCell(2).setCellValue(achievements.getWeight());
                    row.getCell(2).setCellStyle(style2);
                }

                if(achievements.getQuality()==null){
                    row.createCell(3).setCellValue("");
                    row.getCell(3).setCellStyle(style2);
                }else {
                    row.createCell(3).setCellValue(achievements.getQuality());
                    row.getCell(3).setCellStyle(style2);
                }

                if(achievements.getQualityweight()==null){
                    row.createCell(4).setCellValue("");
                    row.getCell(4).setCellStyle(style2);
                }else {
                    row.createCell(4).setCellValue(achievements.getQualityweight());
                    row.getCell(4).setCellStyle(style2);
                }

                if(achievements.getPassrate()==null){
                    row.createCell(5).setCellValue("");
                    row.getCell(5).setCellStyle(style2);
                }else {
                    row.createCell(5).setCellValue(achievements.getPassrate());
                    row.getCell(5).setCellStyle(style2);
                }

                if(achievements.getMaintain()==null){
                    row.createCell(6).setCellValue("");
                    row.getCell(6).setCellStyle(style2);
                }else {
                    row.createCell(6).setCellValue(achievements.getMaintain());
                    row.getCell(6).setCellStyle(style2);
                }

                if(achievements.getMaintainweight()==null){
                    row.createCell(7).setCellValue("");
                    row.getCell(7).setCellStyle(style2);
                }else {
                    row.createCell(7).setCellValue(achievements.getMaintainweight());
                    row.getCell(7).setCellStyle(style2);
                }

                if(achievements.getSynthesis()==null){
                    row.createCell(8).setCellValue("");
                    row.getCell(8).setCellStyle(style2);
                }else {
                    row.createCell(8).setCellValue(achievements.getSynthesis());
                    row.getCell(8).setCellStyle(style2);
                }

                if(achievements.getPrize()==null){
                    row.createCell(9).setCellValue("");
                    row.getCell(9).setCellStyle(style2);
                }else {
                    row.createCell(9).setCellValue(achievements.getPrize());
                    row.getCell(9).setCellStyle(style2);
                }

                if(achievements.getPenalty()==null){
                    row.createCell(10).setCellValue("");
                    row.getCell(10).setCellStyle(style2);
                }else {
                    row.createCell(10).setCellValue(achievements.getPenalty());
                    row.getCell(10).setCellStyle(style2);
                }

                if(achievements.getIntegral()==null){
                    row.createCell(11).setCellValue("");
                    row.getCell(11).setCellStyle(style2);
                }else {
                    row.createCell(11).setCellValue(achievements.getIntegral());
                    row.getCell(11).setCellStyle(style2);
                }

                row.createCell(12).setCellValue(achievements.getShoulddays());
                row.getCell(12).setCellStyle(style2);
                row.createCell(13).setCellValue(achievements.getActualdays());
                row.getCell(13).setCellStyle(style2);
                row.createCell(14).setCellValue(achievements.getBasepay());
                row.getCell(14).setCellStyle(style2);

                if(achievements.getMeritspay()==null){
                    row.createCell(15).setCellValue("");
                    row.getCell(15).setCellStyle(style2);
                }else {
                    row.createCell(15).setCellValue(achievements.getMeritspay());
                    row.getCell(15).setCellStyle(style2);
                }

                row.createCell(16).setCellValue(achievements.getCompetencylevel());
                row.getCell(16).setCellStyle(style2);
                row.createCell(17).setCellValue(achievements.getMonthlylevel());
                row.getCell(17).setCellStyle(style2);
                row.setRowStyle(style2);
            }

            CellRangeAddress cra1 = new CellRangeAddress(row.getRowNum() + 2,row.getRowNum() + 6,1,17);
            sheet.addMergedRegion(cra1);

            row = sheet.createRow(row.getRowNum() + 2);
            Cell cell1 = row.createCell(0);
            cell1.setCellValue("备注:");
            cell1.setCellStyle(style1);

            cell1 = row.createCell(1);
            cell1.setCellValue(remarks);
            cell1.setCellStyle(style3);

            cra1 = new CellRangeAddress(row.getRowNum() + 7,row.getRowNum() + 7,2,4);
            sheet.addMergedRegion(cra1);
            cra1 = new CellRangeAddress(row.getRowNum() + 7,row.getRowNum() + 7,7,10);
            sheet.addMergedRegion(cra1);
            cra1 = new CellRangeAddress(row.getRowNum() + 7,row.getRowNum() + 7,15,17);
            sheet.addMergedRegion(cra1);

            row = sheet.createRow(row.getRowNum() + 7);
            cell1 = row.createCell(1);
            cell1.setCellValue("制表人:");
            cell1.setCellStyle(style1);

            cell1 = row.createCell(2);
            cell1.setCellValue(createname);
            cell1.setCellStyle(style2);

            cell1 = row.createCell(6);
            cell1.setCellValue("审核:");
            cell1.setCellStyle(style1);

            cell1 = row.createCell(7);
            cell1.setCellValue(approval);
            cell1.setCellStyle(style2);

            cell1 = row.createCell(14);
            cell1.setCellValue("批准:");
            cell1.setCellStyle(style1);

            cell1 = row.createCell(15);
            cell1.setCellValue(examine);
            cell1.setCellStyle(style2);

            cra1 = new CellRangeAddress(row.getRowNum() + 1,row.getRowNum() + 1,2,4);
            sheet.addMergedRegion(cra1);
            cra1 = new CellRangeAddress(row.getRowNum() + 1,row.getRowNum() + 1,7,10);
            sheet.addMergedRegion(cra1);
            cra1 = new CellRangeAddress(row.getRowNum() + 1,row.getRowNum() + 1,15,17);
            sheet.addMergedRegion(cra1);

            row = sheet.createRow(row.getRowNum() + 1);

            cell1 = row.createCell(2);
            cell1.setCellValue(createtime);
            cell1.setCellStyle(style2);

            cell1 = row.createCell(7);
            cell1.setCellValue(approvaltime);
            cell1.setCellStyle(style2);

            cell1 = row.createCell(15);
            cell1.setCellValue(examinetime);
            cell1.setCellStyle(style2);



            return wb;
        }
大家好,我是陈亮,JAVA将数据按指定格式导出EXCEL和从EXCEL读相关的内容视频教程1,把poi-2.5.1-final-20040804.jar复制到jdk的classpath下,我这里用的插件是POI我已打好在包中。。我这里JDK装到C:\jdk1.5.0_05\lib 就复制到这里。。先看我演示。。。先演示写出到EXCEL 看到了吧。。。。再演示读EXCEL 这里我是把bb.xls 里面的name:后面的读出来,以及age:后面的读出来。。。看。。。改一下name:后面之后再读...这里文件改为bbc.xls用JCreator方便一点,,所以没用jbuilder OK看到了吧。。。。我再讲解代码写出我已解啦,,,再说读入。。。代码就是这样,,谢谢。。。希望可以和大家共同学习。。。我的msn:ediku@263.net qq:50143539再见...代码在包里。。读入 import org.apache.poi.hssf.usermodel.*;import org.apache.poi.poifs.filesystem.*;import java.io.*;import java.awt.*;import javax.swing.*;import java.awt.event.*;public class read implements ActionListener{JFrame frame; JLabel label1,label2; JTextField tf1,tf2; JButton bu; JPanel panel; public read() { frame=new JFrame("aaaa"); panel=new JPanel(); label1=new JLabel("姓名:"); tf1=new JTextField(10); label2=new JLabel("年龄:"); tf2=new JTextField(10); bu=new JButton("导入"); panel.add(label1); panel.add(tf1); panel.add(label2); panel.add(tf2); panel.add(bu); frame.getContentPane().add(panel); bu.addActionListener(this); frame.setSize(400,400); frame.show(); } /** * @param args */ public static void main(String args[]) { new read(); } public void actionPerformed(ActionEvent e) { if (e.getSource()==bu) { try { // TODO Auto-generated method stub FileInputStream filein=new FileInputStream("bb.xls"); POIFSFileSystem fs=new POIFSFileSystem(filein); HSSFWorkbook wb=new HSSFWorkbook(fs);// POIFSFileSystem fs=new POIFSFileSystem(new Fi
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值