zoukankan      html  css  js  c++  java
  • Excel表复制、取消工作表保护、解除冻结操作

    最近处理Farpoint保存下来的Excel时,发现表格式有各种问题,下面是修改Excel格式操作到内容,做下笔记 :)

    /// <summary>
    /// Excel表相关操作
    /// </summary>
    /// <param name="firstpath">第一个Excel表路径</param>
    /// <param name="secondpath">第二个Excel表路径</param>
    private void ExcelOperation(string firstpath, string secondpath)
    {
        try
        {
            Microsoft.Office.Interop.Excel.Application App = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel.Workbook fWorkbook = App.Workbooks.Open(firstpath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            Microsoft.Office.Interop.Excel.Workbook sWorkbook = App.Workbooks.Open(secondpath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            App.DisplayAlerts = false;  //屏蔽操作提示
            try
            {
                ((Microsoft.Office.Interop.Excel.Worksheet)sWorkbook.Sheets[1]).Delete();  //删除第二个表第一个Sheet
                sWorkbook.Save();

                Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)fWorkbook.Sheets[1];  //获取第一个表第一个Sheet

                sheet.Unprotect(Type.Missing);   //取消工作表保护

                Microsoft.Office.Interop.Excel.Range range = (Microsoft.Office.Interop.Excel.Range)sheet.Rows[1, Type.Missing];  //获取第一行
                sheet.Activate();
                App.ActiveWindow.FreezePanes = false;  //解除冻结
                App.ActiveWindow.SplitColumn = 0;
                App.ActiveWindow.SplitRow = 0;

                range.Interior.ColorIndex = 0;    //设置第一行为白色
                range.Delete(Microsoft.Office.Interop.Excel.XlDirection.xlDown);     //删除第一行

                sheet.Copy(sWorkbook.Sheets[1], Type.Missing);  //复制到第二个表中
                sWorkbook.Save();

            }
            catch (Exception ex)
            {
            }
            finally
            {
                fWorkbook.Close(false, firstpath, null);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(fWorkbook);
                fWorkbook = null;

                sWorkbook.Close(false, secondpath, null);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(sWorkbook);
                sWorkbook = null;

                App.Quit();
                System.Runtime.InteropServices.Marshal.ReleaseComObject(App);
            }
        }
        catch (Exception ex)
        {

        }
    }

  • 相关阅读:
    Spring MVC 程序首页的设置
    绿色版Tomcat 启动 + 停止 + 随系统自动启动
    Lazy Load, 延迟加载图片的 jQuery 插件
    ibatis参数传递小技巧
    SpringMVC接收页面表单参数-java-电脑编程网
    用 JS 点击左右按钮 使图片切换
    JS数组方法汇总 array数组元素的添加和删除
    关于jQuery UI 使用心得及技巧
    MySQL查询in操作 查询结果按in集合顺序显示_Mysql_脚本之家
    JSTL c标签,fn标签,fmt标签
  • 原文地址:https://www.cnblogs.com/Anlycp/p/1947485.html
Copyright © 2011-2022 走看看