示例#1
0
        /// <summary>
        /// 添加图片
        /// </summary>
        /// <param name="dc"></param>
        /// <param name="path"></param>
        public static void AddPicture(this XWPFDocument doc, string path, AddPictureType pType)
        {
            if (path.IndexOf("http") != -1)
            {
                //byte[] bytes = ExcelUtil.getFile(path);
                WebRequest   request  = WebRequest.Create(path);
                WebResponse  response = request.GetResponse();
                Stream       s        = response.GetResponseStream();
                byte[]       data     = new byte[response.ContentLength];
                int          length   = 0;
                MemoryStream ms       = new MemoryStream();
                while ((length = s.Read(data, 0, data.Length)) > 0)
                {
                    ms.Write(data, 0, length);
                }
                ms.Seek(0, SeekOrigin.Begin);

                //using (Stream stt = new MemoryStream(bytes))
                //{
                CT_P p = doc.GetNewP();
                p.SetAlign(ST_Jc.center);
                XWPFParagraph gp  = new XWPFParagraph(p, doc);
                XWPFRun       run = gp.CreateRun();
                run.AddPicture(ms, (int)PictureType.PNG, "2.png", 1000000, 1000000);
                ms.Close();
                //stt.Close();
                //}
            }
            //else if(path.IndexOf(";base64") != -1)
            //{
            //    int index = path.IndexOf(",");
            //    path = path.Substring(index + 1);
            //    byte[] bytes = Convert.FromBase64String(path);
            //    using (Stream s64 = new MemoryStream(bytes))
            //    {
            //        CT_P p = doc.GetNewP();
            //        p.SetAlign(ST_Jc.center);
            //        XWPFParagraph gp = new XWPFParagraph(p, doc);
            //        XWPFRun run = gp.CreateRun();
            //        run.AddPicture(s64, (int)PictureType.PNG, "1.png", 1000000, 1000000);
            //        s64.Close();
            //    }
            //}
            //else
            //{
            //    using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
            //    {
            //        CT_P p = doc.GetNewP();
            //        p.SetAlign(ST_Jc.center);
            //        XWPFParagraph gp = new XWPFParagraph(p, doc);
            //        XWPFRun run = gp.CreateRun();
            //        run.AddPicture(fs, (int)PictureType.PNG, "1.png", 1000000, 1000000);
            //        fs.Close();
            //    }
            //}
        }