项目中往已有表格中存放内容(包含图片)

3 篇文章 0 订阅

代码如下:

 FileInputStream in = new FileInputStream(System.getProperty("user.dir")+"\\file\\测试报告.xls");
        BufferedImage bufferImg = null;
        try {
            HSSFWorkbook work = new HSSFWorkbook(in);
            HSSFSheet sheet = work.getSheetAt(0);
            //图片
            HSSFSheet sheet1 = work.getSheetAt(1);
            HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();
            //依赖信息
            HSSFRow row = sheet.getRow(7 );
            row.getCell(3).setCellValue("1");
            row = sheet.getRow(8);
            row.getCell(3).setCellValue("2");
            row=sheet.getRow(9);
            row.getCell(3).setCellValue("3");
            row=sheet.getRow(10);
            row.getCell(3).setCellValue("4");
            //追溯信息
            row=sheet.getRow(7);
            row.getCell(8).setCellValue("5");
            row=sheet.getRow(8);
            row.getCell(8).setCellValue("6");
            row=sheet.getRow(9);
            row.getCell(8).setCellValue("7");
            row=sheet.getRow(10);
            row.getCell(8).setCellValue("8");
            //检测结果
            row = sheet.getRow(0+13);
            row.getCell(1).setCellValue("9");
            row.getCell(2).setCellValue("10");
            row.getCell(4).setCellValue("11");
            row.getCell(6).setCellValue("12");
            row.getCell(7).setCellValue("13");
            row.getCell(8).setCellValue("14");
            row.getCell(9).setCellValue("15");
            row.getCell(10).setCellValue("16");
            row.getCell(11).setCellValue("17");
            row.getCell(12).setCellValue("18");
            row.getCell(14).setCellValue("19");
            ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
            FileInputStream stream = new FileInputStream(System.getProperty("user.dir")+"\\images\\test\\test.png");
                bufferImg = ImageIO.read(stream);
                ImageIO.write(bufferImg, "png", byteArrayOut);
                //row.getCell(6).setCellValue(detection1ProjectEntity.getCharacter());
                HSSFClientAnchor anchor = new HSSFClientAnchor(10, 10, 0, 0,(short) 3, 3, (short) 8, 8);
                anchor.setAnchorType(3);
                //插入图片
                patriarch.createPicture(anchor, work.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));
                stream.close();
            row =sheet.getRow(3);
            row.getCell(10).setCellValue("认可");
            row.getCell(12).setCellValue("确认");
            row.getCell(14).setCellValue("测试");
            row =sheet.getRow(4);
            row.getCell(11).setCellValue("完成日期");
            row.getCell(14).setCellValue("文件编号");
            OutputStream out = new FileOutputStream("C:\\test\\测试-文件.xls");
            work.setForceFormulaRecalculation(true);
            work.write(out);
            out.flush();
            out.close();
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

附加方法:

    //创建行
    public static void insertRow(HSSFWorkbook wb, HSSFSheet sheet, int starRow,int rows){
        sheet.shiftRows(starRow + 1, sheet.getLastRowNum(), rows,true,false);
        starRow = starRow - 1;
        for (int i = 0; i < rows; i++) {
            HSSFRow sourceRow = null;
            HSSFRow targetRow = null;
            HSSFCell sourceCell = null;
            HSSFCell targetCell = null;
            short m;
            starRow = starRow + 1;
            sourceRow = sheet.getRow(starRow);
            targetRow = sheet.createRow(starRow + 1);
            targetRow.setHeight(sourceRow.getHeight());
            //targetRow.setRowStyle(sourceRow.getRowStyle());
            for (m = sourceRow.getFirstCellNum(); m < sourceRow.getLastCellNum(); m++) {
                sourceCell = sourceRow.getCell(m);
                targetCell = targetRow.createCell(m);
                //targetCell.setEncoding(sourceCell.getEncoding());
                targetCell.setCellStyle(sourceCell.getCellStyle());
                targetCell.setCellType(sourceCell.getCellType());
            }
        }

    }

    //判断是否是数字
    public static boolean isNumber(String str){
        Pattern pattern = Pattern.compile("^(-?\\d+)(\\.\\d+)?$");
        Matcher isNum = pattern.matcher(str);
        if( !isNum.matches() ){
            return false;
        }
        return true;
    }

    //设置下划线
    public HSSFRichTextString setLine(String title,String value, HSSFWorkbook wb){
        Font font=wb.createFont();
        font.setUnderline(Font.U_SINGLE);
        HSSFRichTextString hssfRichTextString=new HSSFRichTextString(title+value);
        hssfRichTextString.applyFont(title.length(),(title+value).length(),font);
        return hssfRichTextString;
    }

表格最终会生成sheet1对的表格内容以及sheet2的图片内容

 

觉得辛苦就扫一扫: 

 20190329111927164.png

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
好的,这是一个稍微复杂一些的 C 语言编程问题。下面是实现代码: ```c #include <stdio.h> #include <string.h> struct Student { int id; // 学号 char name[20]; // 姓名 float scores[3]; // 三门课的成绩 float avgScore; // 平均成绩 }; int main() { struct Student stu; printf("请输入学生的信息:\n"); printf("学号:"); scanf("%d", &stu.id); getchar(); // 读取输入缓冲区的回车符 printf("姓名:"); fgets(stu.name, 20, stdin); for (int i = 0; i < 3; i++) { printf("第%d门课的成绩:", i + 1); scanf("%f", &stu.scores[i]); } stu.avgScore = (stu.scores[0] + stu.scores[1] + stu.scores[2]) / 3.0; // 计算平均成绩 FILE *fp = fopen("studdat", "wb"); // 打开文件studdat以进行二进制写操作 if (fp == NULL) { printf("无法打开文件!\n"); return 1; } fwrite(&stu, sizeof(stu), 1, fp); // 将结构体写入文件 fclose(fp); // 关闭文件 printf("学生信息已保存到文件studdat。\n"); // 从文件读取结构体并以表格形式输出 printf("\n学生信息表格:\n"); printf("学号\t姓名\t\t成绩1\t成绩2\t成绩3\t平均分\n"); fp = fopen("studdat", "rb"); // 以二进制读模式打开文件 if (fp == NULL) { printf("无法打开文件!\n"); return 1; } while (fread(&stu, sizeof(stu), 1, fp)) { // 读取文件的结构体 printf("%d\t%s", stu.id, stu.name); for (int i = 0; i < 3; i++) { printf("\t%.2f", stu.scores[i]); } printf("\t%.2f\n", stu.avgScore); } fclose(fp); // 关闭文件 return 0; } ``` 运行程序后,按照提示输入学生的信息,程序会将原有数据和计算出的平均分数存放在磁盘文件“studdat”。然后程序会从文件读取学生信息,并以表格形式在显示器上输出。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Knight-Key

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值