예제 #1
0
        public void CollectJpBtn(string outRoot = null)
        {
            if (string.IsNullOrWhiteSpace(outRoot))
            {
                outRoot = Instance().OutJPDir;
            }

            try
            {
                var outExcelPath    = outRoot + "/" + Name.Substring(Name.LastIndexOf('/') + 1) + ".xlsx";
                var outTemplatePath = outRoot + "/template.xlsx";
                // 从模板创建输出文件
                var outBook  = ExcelUtils.Open(outTemplatePath);
                var outSheet = outBook.Sheet("jp") as XSSFSheet;//new XSSFSheet();//

                var infoSheet = outBook.Sheet("info") as XSSFSheet;

                var hrow = outSheet.GetRow(0);
                var head = new ExcelHead(hrow);

                //遍历文件列表输出到输出文件
                // 每 MaxRowPerSheet 条分一张表
                int  MaxrowPerSheet     = 50000;
                int  outSheetRowIdx     = 0;
                int  outSheetIdx        = 0;
                int  totalCellCount     = 0;
                int  inFileIdx          = 0;
                int  inSheetIdx         = 0;
                int  MaxRowAndColumnNum = 90000;
                long wordCount          = 0;
                long totalWordCount     = 0;
                foreach (var path in Pathes)
                {
                    ++inFileIdx;
                    IWorkbook inbook = ExcelUtils.Open(path);
                    inSheetIdx = 0;

                    var infoCell = infoSheet.Cell(inFileIdx, inSheetIdx);
                    infoCell.SetCellValue(path);

                    foreach (var sheet in inbook.AllSheets())
                    {
                        ++inSheetIdx;
                        for (var ir = 1; ir <= sheet.LastRowNum && ir <= MaxRowAndColumnNum; ++ir)
                        {
                            var row = sheet.GetRow(ir);
                            if (row == null)
                            {
                                continue;
                            }
                            if ((ir % 100) == 0)
                            {
                                EditorUtility.DisplayCancelableProgressBar(
                                    "CollectJp ..." + path.RReplace(".*ExcelData/", "")
                                    , sheet.SheetName + ": " + ir + "/" + sheet.LastRowNum
                                    , (float)(inFileIdx) / Pathes.Count());
                            }

                            for (int ic = 0; ic < row.LastCellNum && ic <= MaxRowAndColumnNum; ++ic)
                            {
                                // // 分表
                                // if (outSheetRowIdx > MaxrowPerSheet)
                                // {
                                //     ++outSheetIdx;
                                //     outSheetRowIdx = 0;
                                //     outSheet = outBook.CreateSheet("jp_" + outSheetIdx) as XSSFSheet;
                                //     // add head
                                //     var h = outSheet.Row(0);
                                //     h.RowStyle = hrow.RowStyle;
                                //     for (var hi = 0; hi < hrow.LastCellNum; ++hi)
                                //     {
                                //         h.Cell(hi).SetCellValue(hrow.Cell(hi).SafeSValue());
                                //         outSheet.SetColumnWidth(hi, 1500);
                                //     }
                                // }

                                var v = row.Cell(ic).SValueOneline();
                                // AppLog.d(Tag, "{0}: {1}", v, v.Length);
                                var matches = Regex.Matches(v, JPRegular + "+.*");
                                if (matches.Count > 0)
                                {
                                    ++totalCellCount;
                                    ++outSheetRowIdx;

                                    var c = outSheet.Cell(outSheetRowIdx, head[HeadIdx.jp]);
                                    // 去重引用
                                    var iorow = outSheet.Contain(r => r.Cell(head[HeadIdx.jp]).SValueOneline() == v);
                                    if (iorow > 0)
                                    {
                                        c.SetCellValue(string.Format("${0}", iorow));
                                    }
                                    else
                                    {
                                        wordCount += v.Length;
                                        c.SetCellValue(v);
                                    }
                                    //c.CellStyle.IsLocked = cellLock;
                                    totalWordCount += v.Length;

                                    c = outSheet.Cell(outSheetRowIdx, head[HeadIdx.trans]);
                                    c.SetCellValue("译文");
                                    //c.CellStyle.IsLocked = false;

                                    c = outSheet.Cell(outSheetRowIdx, head[HeadIdx.trans_jd]);
                                    c.SetCellValue("校对");
                                    //c.CellStyle.IsLocked = false;

                                    c = outSheet.Cell(outSheetRowIdx, head[HeadIdx.i]);
                                    c.SetCellValue(ir);
                                    //c.CellStyle.IsLocked = cellLock;

                                    c = outSheet.Cell(outSheetRowIdx, head[HeadIdx.j]);
                                    c.SetCellValue(ic);
                                    //c.CellStyle.IsLocked = cellLock;

                                    c = outSheet.Cell(outSheetRowIdx, head[HeadIdx.SheetName]);
                                    c.SetCellValue(sheet.SheetName);
                                    //c.CellStyle.IsLocked = cellLock;

                                    c = outSheet.Cell(outSheetRowIdx, head[HeadIdx.FilePath]);
                                    c.SetCellValue(path);
                                }
                            } // cell
                        }     // row

                        // add file sheet info to info sheet
                        // one line per file
                        infoCell = infoSheet.Cell(inFileIdx, inSheetIdx);
                        infoCell.SetCellValue(sheet.SheetName);
                    } // sheet
                }     // path

                infoSheet.Cell(inFileIdx + 1, 0).SetCellValue("wordCount");
                infoSheet.Cell(inFileIdx + 1, 1).SetCellValue(wordCount);

                var outStream = new FileStream(outExcelPath, FileMode.Create);
                outBook.Write(outStream);
                AppLog.d(Tag, "CollectJp: " + outExcelPath);

                CountWordUniq = wordCount;
                CountWord     = totalWordCount;
            }
            finally
            {
                EditorUtility.ClearProgressBar();
            }
        }