往xls 里加条形 并固定位置

  private void InputTOExcel()

        {
                HSSFSheet excelSheet = (HSSFSheet)hssfworkbook.GetSheetAt(index);//获得模版的第一个sheet
                //HSSFPatriarch patriarch = (HSSFPatriarch)excelSheet.CreateDrawingPatriarch();//每个Excel只能创建一个画板
                hssfworkbook.SetSheetName(index, itemCode);  //设置sheet名称
                excelSheet.DisplayGridlines = false;  //隐藏网格线
              

 

               //根据流生成条形码

                    System.IO.MemoryStream strea = CreateBarcode.Create39Barcode("123456789", 0);   //条形码生成的参数和,是否否缩略图片

                    //sheet//流 //列 //行 //列 //行
                    SetXlsImage(excelSheet, strea, 4, 3, 7, 3);   //固定到xls的几行几列
                    strea.Dispose();

       }

 

#region 固定条形码的位置
        private void SetXlsImage(Sheet sheet, System.IO.MemoryStream ms, int y1, int x1, int y2, int x2)
        {
            if (patriarch == null || sheet.SheetName != patriarch.sheet.SheetName)
            {
                patriarch = (HSSFPatriarch)sheet.CreateDrawingPatriarch();
            }
            //create the anchor
            HSSFClientAnchor anchor;
            anchor = new HSSFClientAnchor(0, 0, 0, 0, y1, x1, y2, x2);
            //anchor.AnchorType = 3;s
            //load the picture and get the picture index in the workbook
            //HSSFPicture picture = (HSSFPicture)patriarch.CreatePicture
            //    (anchor, LoadImage(@"d:\temp\pdf.jpg", hssfworkbook));

            picture = (HSSFPicture)patriarch.CreatePicture
               (anchor, LoadImage(ms, hssfworkbook));

            picture.Resize();
            //picts.Add(picture);

        }

        /// <summary>
        /// ファイルを読み込み By MemoryStream
        /// </summary>
        /// <param name="file"></param>
        /// <param name="wb"></param>
        /// <returns></returns>
        public int LoadImage(System.IO.MemoryStream file, HSSFWorkbook wb)
        {
            byte[] buffer = file.ToArray();
            return wb.AddPicture(buffer, PictureType.PNG);
        }
        #endregion


        private void CreateCell(Row row, int colIndex, string text, CellStyle style)
        {
            row.CreateCell(colIndex).SetCellValue(text);
            row.GetCell(colIndex).CellStyle = style;
        }

        #endregion

 

 

 

/// <summary>
    /// 生成Code39
    /// </summary>
    /// <param name="code"></param>
    /// <returns></returns>
    public static System.IO.MemoryStream Create39Barcode(string code,int sizekey)
    {
        DeltaCat.BarCode.Code39 c39 = new DeltaCat.BarCode.Code39();
        c39.BarCodeFontSzie = 38;
        c39.BarCodeValue = code;
        c39.ShowBarCodeValue = true;

       
        System.IO.MemoryStream ms = (System.IO.MemoryStream)c39.CreateWinForm(System.Drawing.Imaging.ImageFormat.Png);

        if (sizekey > 0)
        {
            System.Drawing.Image Picture = System.Drawing.Image.FromStream(ms);

            YzImageProc.ImageReSize ir = new YzImageProc.ImageReSize(Picture);
            ir.SizeKey = sizekey;
            System.Drawing.Image thumb = ir.MakeSLTV2(Picture, ir.SizeKey);
            System.IO.MemoryStream ms2 = new MemoryStream();
            thumb.Save(ms2, System.Drawing.Imaging.ImageFormat.Png);

            ms.Dispose();

            return ms2;
        }
        return ms;
    }

 

/// <summary>
    /// 生成SKU条形码
    /// </summary>
    /// <param name="item">商品号 如: 800000</param>
    /// <param name="response">尺码 如:0101</param>
    /// <returns></returns>
    public static System.IO.MemoryStream Create128Barcode(string item, HttpResponse response)
    {
        // 引数取得
        string normid = item.Substring(0, 6);
        string itemid = item.Substring(6);

        System.IO.MemoryStream MS = null;


        // 引数Check
        if (!string.IsNullOrEmpty(normid) || !string.IsNullOrEmpty(itemid))
        {
            string country = "20";
            int width = 120;
            int height = 50;
            float size = 1.1f;
            //Font font = new Font(FontFamily.GenericMonospace, 10);
            Font font = new Font("MS Gothic", 9);

            // 出力設定
            response.ClearContent();
            response.ContentType = "image/png";

            string janCode = ComMethod.getJanCode(country + normid + itemid);

            MS = new System.IO.MemoryStream();

            // 使用方法
            System.Drawing.Image imgTar = BarCodeLibEan13.BarCodeMaker.SetBarCodeImg
            (country, normid, itemid, janCode.Substring(12, 1), width, height, size, 0, 0, font);

            imgTar.Save(MS, System.Drawing.Imaging.ImageFormat.Png);

            // 出力
            //response.BinaryWrite(MS.GetBuffer());
            resを回収
            //imgTar.Dispose();
            //MS.Dispose();
           
        }
        return MS;
    }

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值