使用NPOI将多张图片导入execl

 protected void btn_Export_Click(object sender, EventArgs e)
    {
        List<BNXX_SJXJ_XJSJ> list = ViewState["data"] as List<BNXX_SJXJ_XJSJ>;


        string[] AppPaths = HttpRuntime.AppDomainAppPath.ToString().Split('\\');
        string PicPath = string.Empty;
        for (int i = 0; i < AppPaths.Length - 2; i++)
        {
            PicPath = PicPath + "/" + AppPaths[i];
        }
        PicPath = PicPath.Substring(1) + "/mytest/Photos";
        ///调用模板,先保存,后下载
        string path = Server.MapPath("../../template/SJingSJ.xls");
        using (FileStream fs = File.OpenRead(path))
        {
            using (Workbook wk = new HSSFWorkbook(fs))
            {
                int pictureIndex1 = AddPictureToWorkbook(list[0].XQSY, PicPath, wk);
                int pictureIndex2 = AddPictureToWorkbook(list[0].XZSY, PicPath, wk);
                int pictureIndex3 = AddPictureToWorkbook(list[0].XHSY, PicPath, wk);

                using (Sheet sheet = wk.GetSheetAt(0))
                {
                    sheet.GetRow(1).GetCell(3).SetCellValue(list[0].JH);
                    sheet.GetRow(1).GetCell(10).SetCellValue(list[0].FZR);
                    sheet.GetRow(1).GetCell(15).SetCellValue(list[0].XJRQ);
                    sheet.GetRow(2).GetCell(6).SetCellValue(list[0].GCZSQDYL1);
                    sheet.GetRow(3).GetCell(6).SetCellValue(list[0].GCZSQDYL2);
                    sheet.GetRow(4).GetCell(6).SetCellValue(list[0].GCZSQDYL3);
                    sheet.GetRow(5).GetCell(6).SetCellValue(list[0].GY);
                    sheet.GetRow(6).GetCell(6).SetCellValue(list[0].YY);
                    sheet.GetRow(7).GetCell(6).SetCellValue(list[0].PZ);
                    sheet.GetRow(8).GetCell(6).SetCellValue(list[0].SZ);
                    sheet.GetRow(9).GetCell(6).SetCellValue(list[0].XJYYJMB);
                    sheet.GetRow(2).GetCell(13).SetCellValue(list[0].LC);
                    sheet.GetRow(3).GetCell(13).SetCellValue(list[0].XJYB);
                    sheet.GetRow(4).GetCell(13).SetCellValue(list[0].JYGX);
                    sheet.GetRow(5).GetCell(13).SetCellValue(list[0].XQ);
                    sheet.GetRow(6).GetCell(13).SetCellValue(list[0].GCXJ);

                    sheet.GetRow(10).GetCell(5).SetCellValue(list[0].YXJSJ);
                    sheet.GetRow(10).GetCell(10).SetCellValue(list[0].YXJYL);
                    sheet.GetRow(10).GetCell(15).SetCellValue(list[0].YXJPL);

                    sheet.GetRow(11).GetCell(5).SetCellValue(list[0].EXJSJ);
                    sheet.GetRow(11).GetCell(10).SetCellValue(list[0].EXJYL);
                    sheet.GetRow(11).GetCell(15).SetCellValue(list[0].EXJPL);

                    sheet.GetRow(12).GetCell(5).SetCellValue(list[0].SXJSJ);
                    sheet.GetRow(12).GetCell(10).SetCellValue(list[0].SXJYL);
                    sheet.GetRow(12).GetCell(15).SetCellValue(list[0].SXJPL);

                    sheet.GetRow(13).GetCell(4).SetCellValue(list[0].YXJSJ2);
                    sheet.GetRow(13).GetCell(10).SetCellValue(list[0].YXJYL2);
                    sheet.GetRow(13).GetCell(15).SetCellValue(list[0].YJKPL);
                    sheet.GetRow(14).GetCell(15).SetCellValue(list[0].YCKPL);

                    sheet.GetRow(15).GetCell(4).SetCellValue(list[0].EXJSJ2);
                    sheet.GetRow(15).GetCell(10).SetCellValue(list[0].EXJYL2);
                    sheet.GetRow(15).GetCell(15).SetCellValue(list[0].EJKPL);
                    sheet.GetRow(16).GetCell(15).SetCellValue(list[0].ECKPL);

                    sheet.GetRow(17).GetCell(4).SetCellValue(list[0].SXJSJ2);
                    sheet.GetRow(17).GetCell(10).SetCellValue(list[0].SXJYL2);
                    sheet.GetRow(17).GetCell(15).SetCellValue(list[0].SJKPL);
                    sheet.GetRow(18).GetCell(15).SetCellValue(list[0].SCKPL);

                    sheet.GetRow(19).GetCell(4).SetCellValue(list[0].XJCH);
                    sheet.GetRow(20).GetCell(4).SetCellValue(list[0].XJXG);

                    sheet.GetRow(23).GetCell(2).SetCellValue(list[0].XQSYSJ);
                    sheet.GetRow(23).GetCell(5).SetCellValue(list[0].XZSYSJ);
                    sheet.GetRow(23).GetCell(12).SetCellValue(list[0].XHSYSJ);


                    InsertPictureToSheet(sheet, pictureIndex1, 2, 24, 4, 24);
                    InsertPictureToSheet(sheet, pictureIndex2, 5, 24, 11, 24);
                    InsertPictureToSheet(sheet, pictureIndex3, 12, 24, 16, 24);

                    //AddPictureToExecl(list[0].XQSY, PicPath, wk, sheet, 2, 24, 4, 24);
                    //AddPictureToExecl(list[0].XZSY, PicPath, wk, sheet, 5, 24, 11, 24);
                    //AddPictureToExecl(list[0].XHSY, PicPath, wk, sheet, 12, 24, 16, 24);
                    string outputPath = Server.MapPath("../../OutPut/注水井洗井设计报表.xls");
                    using (FileStream files = new FileStream(outputPath, FileMode.Create))
                    {
                        wk.Write(files);
                    }

                    Response.Clear();
                    Response.Buffer = true;
                    Response.Charset = "utf-8";
                    Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("注水井洗井设计报表", System.Text.Encoding.UTF8) + ".xls");
                    Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");     //设置输出流为简体中文
                    Response.ContentType = "application/ms-execl";    //设置输出文件类型为excel文件。 
                    Response.WriteFile(outputPath);
                    Response.End();
                }
            }
        }
    }
View Code
 //2, 24, 4, 24
    private int AddPictureToWorkbook(string picName, string PicPath, Workbook wk)
    {
        bool isExists = true;
        if (!string.IsNullOrEmpty(picName))
        {
            PicPath = PicPath + "/" + picName;
            isExists = Directory.Exists(PicPath);
        }
        else
        {
            isExists = false;
        }
        if (!isExists)
        {
            PicPath = Server.MapPath("../../Images/Button/Defalut .jpg");
        }
        byte[] bytes = System.IO.File.ReadAllBytes(PicPath);
        int pictureIdx = wk.AddPicture(bytes, PictureType.JPEG);
        return pictureIdx;
    }
View Code
private void InsertPictureToSheet(Sheet sheet, int pictureIdx, int StartX, int StartY, int EndX, int EndY)
    {
        Drawing patriarch = sheet.CreateDrawingPatriarch();
        HSSFClientAnchor anchor = new HSSFClientAnchor(255, 125, 1023, 150, StartX, StartY, EndX, EndY);
        Picture pict = patriarch.CreatePicture(anchor, pictureIdx);
        pict.Resize(1);
    }
View Code

 

转载于:https://www.cnblogs.com/gotoschool/p/4103063.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值