Exemplo n.º 1
0
        private void DelHiddenWs_Click(object sender, EventArgs e)
        {
            TryRun(() =>
            {
                string filePath = filePath1.Text.Trim().移除路径前后引号();
                if (string.IsNullOrEmpty(filePath))
                {
                    MessageBox.Show("路径不能为空");
                    return;
                }
                if (string.Compare(".xlsx", System.IO.Path.GetExtension(filePath), true) != 0)
                {
                    MessageBox.Show("只支持.xlsx文件");
                    return;
                }
                using (var ms = new MemoryStream())
                    using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                        using (var excelPackage = new ExcelPackage(fs))
                        {
                            EPPlusHelper.DeleteWorksheet(excelPackage, eWorkSheetHidden.Hidden, eWorkSheetHidden.VeryHidden);
                            excelPackage.SaveAs(ms);
                            ms.Position = 0;

                            var fileDir        = Path.GetDirectoryName(filePath);
                            var fileName       = Path.GetFileNameWithoutExtension(filePath);
                            string filePathOut = Path.Combine(fileDir, $"{fileName}_OnlyVisibleWS.xlsx");
                            ms.Save(filePathOut);
                            MessageBox.Show($"文件已经生成,在目录'{fileDir}'");
                            WinFormHelper.OpenDirectory(fileDir);
                        }
            });
        }