Beispiel #1
0
        //读取Excel 图片
        private void button1_Click(object sender, EventArgs e)
        {
            NPOI.SS.UserModel.ISheet sheet = null;//工作表
            var fs       = new FileStream(txtExcelName.Text, FileMode.Open, FileAccess.Read);
            var workbook = new XSSFWorkbook(fs);

            for (int i = 0; i < Convert.ToInt32(txtNumber.Text); i++)
            {
                sheet = workbook.GetSheetAt(i);

                var list  = sheet.GetAllPictureInfos();
                var data  = ExcelToDataTable(sheet);
                int index = 0;

                var FilePath = DateTime.Now.ToString("yyyyMMdd") + sheet.SheetName;
                if (!System.IO.Directory.Exists(FilePath))
                {
                    //创建pic文件夹
                    System.IO.Directory.CreateDirectory(FilePath);
                }
                else
                {
                    System.IO.Directory.Delete(FilePath, true);
                    System.IO.Directory.CreateDirectory(FilePath);
                }

                foreach (var item in list)
                {
                    try
                    {
                        index++;
                        int row  = item.MinRow;
                        var info = data.Rows[row - 1];
                        var name = "";;
                        for (var j = 0; j < Convert.ToInt32(txtColCount.Text); j++)
                        {
                            name += info.ItemArray[j].ToString();
                        }
                        name += "_" + index.ToString() + ".png";
                        //var name = info.ItemArray[0].ToString() + info.ItemArray[1].ToString() + info.ItemArray[2].ToString() + info.ItemArray[3].ToString() + info.ItemArray[4].ToString() + "_" + index.ToString() + ".png";
                        //单品序号+品牌+渠道+达人账号这样命名
                        name = name.Replace("/", "");
                        writePic(item.PictureData, name, FilePath);
                    }
                    catch (Exception)
                    {
                        continue;
                    }
                }
                //重名的怎么处理
            }



            MessageBox.Show("读取完成");
        }
 /// <summary>
 /// 获取工作表中包含图片的信息列表
 /// </summary>
 /// <param name="sheet">NPOI工作表</param>
 public static List <PictureInfo> GetAllPictureInfos(this NPOI.SS.UserModel.ISheet sheet) => sheet.GetAllPictureInfos(null, null, null, null);