zoukankan      html  css  js  c++  java
  • C#:将.csv格式文件转换成.xlsx格式文件

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.Drawing;
    using System.Linq;
    using System.Reflection;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using Excel = Microsoft.Office.Interop.Excel;
    
    namespace SettlementResolve
    {
        public partial class JDSettlement : Form
        {
            public JDSettlement()
            {
                InitializeComponent();
            }
    
            private void txtbox_ReadOnlyChanged(object sender, EventArgs e)
            {
                txtbox.ReadOnly = true;
            }
    
            private void JDSettlement_Load(object sender, EventArgs e)
            {
    
            }
    
            /// <summary>
            /// 将.csv文件转换成.xlsx文件
            /// </summary>
            /// <param name="FilePath">.csv文件绝对路径</param>
            /// <returns>返回转换后的.xlsx文件路径</returns>
            public static string CSVSaveasXLSX(string FilePath)
            {
                QuertExcel();
                string NewFilePath = "";
    
                Excel.Application excelApplication;
                Excel.Workbooks excelWorkBooks = null;
                Excel.Workbook excelWorkBook = null;
                Excel.Worksheet excelWorkSheet = null;
    
                try
                {
                    excelApplication = new Excel.Application();
                    excelWorkBooks = excelApplication.Workbooks;
                    excelWorkBook=((Excel.Workbook) excelWorkBooks.Open(FilePath,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,
                        Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value));
                    excelWorkSheet = (Excel.Worksheet)excelWorkBook.Worksheets[1];
                    excelApplication.Visible = false;
                    excelApplication.DisplayAlerts = false;
                    NewFilePath = FilePath.Replace(".csv", ".xlsx");
                    excelWorkBook.SaveAs(NewFilePath, Excel.XlFileFormat.xlAddIn, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value,
                        Missing.Value, Missing.Value, Missing.Value, Missing.Value);
                    excelWorkBook.Close();
                    QuertExcel();
                    GC.Collect(System.GC.GetGeneration(excelWorkSheet));
                    GC.Collect(System.GC.GetGeneration(excelWorkBook));
                    GC.Collect(System.GC.GetGeneration(excelApplication));
                }
                catch (Exception exc)
                {
                    throw new Exception(exc.Message);
                }
    
                finally
                {
                    GC.Collect();
                }
                return NewFilePath;
            }
    
            /// <summary>
            /// 执行过程中可能会打开多个EXCEL文件,所以Kill掉
            /// </summary>
            private static void QuertExcel()
            {
                Process[] excels = Process.GetProcessesByName("EXCEL");
                foreach (var item in excels)
                {
                    item.Kill();
                }
            }
        }
    }


    原文转载自:http://www.cnblogs.com/junjie94wan/archive/2013/05/23/3094483.html

    原文有个地方被我修改过来,在CSVSaveasXLS方法中
    excelApplication = new Excel.ApplicationClass();
    这行会出现以下的问题:

    导致整个程序无法运行,所以我稍作修改成我以上所写的代码,运行调试后,暂无发现明显问题。

  • 相关阅读:
    人生中对我影响最大的三位老师
    自我介绍
    对我影响较大的三位老师
    自我介绍
    Java入门到精通——基础篇之static关键字
    天猫优惠券面值可以随意修改
    常用的PC/SC接口函数
    批量删除本地指定扩展名文件工具
    算法:C++排列组合
    Java入门到精通——基础篇之面向对象
  • 原文地址:https://www.cnblogs.com/W--Jing/p/7977236.html
Copyright © 2011-2022 走看看