Loading

记一次Word 表格处理

    private static void Main(string[] args)
    {
    
        //假定,每个表格都只有三张照片 且 按以下方式 排列

        /*
                    "GIS平面图"
                    "GIS平面图"
                    "GIS平面图"
        -----------------------------------
        "照片(井盖近景)" | "照片(井盖全景)"
        "照片(井盖近景)" | "照片(井盖全景)"
        "照片(井盖近景)" | "照片(井盖全景)"

        */

        
        Dictionary<int, string> imgname = new Dictionary<int, string>();
        imgname.Add(0, "GIS平面图");
        imgname.Add(1, "照片(井盖近景)");
        imgname.Add(2, "照片(井盖全景)");

        DirectoryInfo directoryInfo = new DirectoryInfo("docs");

        //如果多层文件夹嵌套 ,则:            foreach (DirectoryInfo directory in directoryInfo.GetDirectories())

        int seed = 202330000;
        int imgdiridx = 1;

        foreach (var file in directoryInfo.GetFiles())
        {
            if (file.Extension != ".docx")
            {
                continue;
            }
            //~$井盖信息卡东京120路.docx
            if (file.Name.Contains("~$"))
            {
                continue;
            }

            Console.WriteLine("正在处理..." + file.Name);

            Stream stream = File.OpenRead(file.FullName);
            XWPFDocument doc = new XWPFDocument(stream);
            var allTables = doc.Tables;
            int tableidx = 1;
            foreach (var table in allTables)
            {
                //在同一行输出进度
                int currentLineCursor = Console.CursorTop;//记录当前光标位置
                Console.SetCursorPosition(0, Console.CursorTop - 1);//将光标至于当前行的开始位置
                Console.WriteLine("正在处理..." + file.Name + " : 表格 " + tableidx.ToString() + "/" + allTables.Count);//用空格将当前行填满,相当于清除当前行

                Console.SetCursorPosition(0, currentLineCursor);//将光标恢复至开始时的位置

                int index = 0;
                string imageName = null;

                string path = "imgs" + "\\" + file.Name + "\\" + (seed + imgdiridx).ToString();

                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }

                foreach (var row in table.Rows)
                {
                    var cells = row.GetTableCells();
                    foreach (var cell in cells)
                    {
                        foreach (var paragraphs in cell.Paragraphs)
                        {
                            foreach (var run in paragraphs.Runs)
                            {
                                List<XWPFPicture> xwpfPictureList = run.GetEmbeddedPictures();
                                if (xwpfPictureList.Count > 0)
                                {
                                    var data = xwpfPictureList[0].GetPictureData();
                                    var picData = data.Data;
                                    var image = GetImageFromByte(picData);
                                    imageName = $"{imgname[index]}.png";

                                    image.Save(path + "\\" + imageName, ImageFormat.Png);
                                    index++;
                                }
                            }
                        }
                    }
                }
                tableidx++;
                imgdiridx++;
            }

            Console.WriteLine(file.Name + "   处理完成。");
        }

 
      

        Console.WriteLine(":*:((( ¨)/。.:*:・'☆恭喜☆");
        Console.ReadKey();
    }

    public static System.Drawing.Image GetImageFromByte(byte[] streamByte)
    {
        System.IO.MemoryStream ms = new System.IO.MemoryStream(streamByte);
        System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
        return img;
    }
posted @ 2023-10-10 13:14  可是我爱你啊  阅读(5)  评论(1编辑  收藏  举报