保存,将Excel文件另存为指定格式

时间:2021-12-26 12:54:30

 /// <summary>
  /// 保存文件
  /// </summary>
  public void SaveFile()
  {
   try
   {
    workBook.Save();   
   }
   catch(Exception e)
   {
    throw e;
   }
   finally
   {
    this.Dispose();
   }
  }

  /// <summary>
  /// 另存文件
  /// </summary>
  public void SaveAsFile()
  {
   if(this.outputFile == null)
    throw new Exception("没有指定输出文件路径!");

   try
   {
    workBook.SaveAs(outputFile,missing,missing,missing,missing,missing,Excel.XlSaveAsAccessMode.xlExclusive,missing,missing,missing,missing);   
   }
   catch(Exception e)
   {
    throw e;
   }
   finally
   {
    this.Dispose();
   }
  }

  /// <summary>
  /// 将Excel文件另存为指定格式
  /// </summary>
  /// <param name="format">HTML,CSV,TEXT,EXCEL,XML</param>
  public void SaveAsFile(string format)
  {
   if(this.outputFile == null)
    throw new Exception("没有指定输出文件路径!");

   try
   {
    switch(format)
    {
     case "HTML":
     {
      workBook.SaveAs(outputFile,Excel.XlFileFormat.xlHtml,missing,missing,missing,missing,Excel.XlSaveAsAccessMode.xlExclusive,missing,missing,missing,missing);   
      break;
     }
     case "CSV":
     {
      workBook.SaveAs(outputFile,Excel.XlFileFormat.xlCSV,missing,missing,missing,missing,Excel.XlSaveAsAccessMode.xlExclusive,missing,missing,missing,missing);   
      break;
     }
     case "TEXT":
     {
      workBook.SaveAs(outputFile,Excel.XlFileFormat.xlHtml,missing,missing,missing,missing,Excel.XlSaveAsAccessMode.xlExclusive,missing,missing,missing,missing);   
      break;
     }
//     case "XML":
//     {
//      workBook.SaveAs(outputFile,Excel.XlFileFormat.xlXMLSpreadsheet, Type.Missing, Type.Missing,
//       Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange,
//       Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
//      break;
//     }
     default:
     {
      workBook.SaveAs(outputFile,missing,missing,missing,missing,missing,Excel.XlSaveAsAccessMode.xlExclusive,missing,missing,missing,missing);   
      break;
     }
    }
   }
   catch(Exception e)
   {
    throw e;
   }
   finally
   {
    this.Dispose();
   }
  }

  /// <summary>
  /// 另存文件
  /// </summary>
  /// <param name="fileName">文件名</param>
  public void SaveFile(string fileName)
  {
   try
   {
    workBook.SaveAs(fileName,missing,missing,missing,missing,missing,Excel.XlSaveAsAccessMode.xlExclusive,missing,missing,missing,missing);   
   }
   catch(Exception e)
   {
    throw e;
   }
   finally
   {
    this.Dispose();
   }
  }

  /// <summary>
  /// 将Excel文件另存为指定格式
  /// </summary>
  /// <param name="fileName">文件名</param>
  /// <param name="format">HTML,CSV,TEXT,EXCEL,XML</param>
  public void SaveAsFile(string fileName,string format)
  {
   try
   {
    switch(format)
    {
     case "HTML":
     {
      workBook.SaveAs(fileName,Excel.XlFileFormat.xlHtml,missing,missing,missing,missing,Excel.XlSaveAsAccessMode.xlExclusive,missing,missing,missing,missing);   
      break;
     }
     case "CSV":
     {
      workBook.SaveAs(fileName,Excel.XlFileFormat.xlCSV,missing,missing,missing,missing,Excel.XlSaveAsAccessMode.xlExclusive,missing,missing,missing,missing);   
      break;
     }
     case "TEXT":
     {
      workBook.SaveAs(fileName,Excel.XlFileFormat.xlHtml,missing,missing,missing,missing,Excel.XlSaveAsAccessMode.xlExclusive,missing,missing,missing,missing);   
      break;
     }
//     case "XML":
//     {
//      workBook.SaveAs(fileName,Excel.XlFileFormat.xlXMLSpreadsheet, Type.Missing, Type.Missing,
//       Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange,
//       Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
//      break;
//     }
     default:
     {
      workBook.SaveAs(fileName,missing,missing,missing,missing,missing,Excel.XlSaveAsAccessMode.xlExclusive,missing,missing,missing,missing);   
      break;
     }
    }
   }
   catch(Exception e)
   {
    throw e;
   }
   finally
   {
    this.Dispose();
   }
  }