往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);

        }

        ///


        /// ファイルを読み込み By MemoryStream
        ///

        ///
        ///
        ///
        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

 

 

 

///


    /// 生成Code39
    ///

    ///
    ///
    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;
    }

 

///


    /// 生成SKU条形码
    ///

    /// 商品号 如: 800000
    /// 尺码 如:0101
    ///
    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;
    }

 

你可能感兴趣的:(ASP.NET)