C#将EXCEL表中的每个表单独保存到本地

来源:互联网 发布:阿里云服务器管理 编辑:程序博客网 时间:2024/06/05 04:36

       OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "excel文件(*.xls,*.xlsx)|*.xls;*.xlsx";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                string filename = dlg.FileName; //包含路径和文件名的全称
                string path = Path.GetDirectoryName(filename);
                string name = Path.GetFileNameWithoutExtension(filename);// 不含路径和后缀的文件名称
                path = path + "\\" + name;
                if (!Directory.Exists(path))
                    Directory.CreateDirectory(path);

                SplashScreenManager.ShowForm(typeof(frmWaiting));
                Workbook source = new Workbook();
                source.LoadDocument(filename);

                foreach (var item in source.Worksheets)
                {
                    Workbook save = new Workbook();
                    save.Worksheets[0].CopyFrom(item);
                    save.Worksheets[0].Name = item.Name;
                    save.SaveDocument(path + "\\" + item.Name + ".xls", DocumentFormat.Xls);
                    save.Dispose();
                }
                source.Dispose();
                SplashScreenManager.CloseForm();
                MessageBox.Show("转换完成");
            }
       
        }

    }

0 0