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();
    这行会出现以下的问题:

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

  • 相关阅读:
    第 45 届国际大学生程序设计竞赛(ICPC)亚洲区域赛(南京)K Co-prime Permutation
    2020 CCPC绵阳站赛后回忆+总结
    CF 1473D Program
    Educational Codeforces Round 100 (Rated for Div. 2) 补题情况
    Codeforces Round #690 (Div. 3) (补题情况)
    这个博客停用,新博客地址:www.baccano.fun
    炫酷路径(牛客)
    洛谷 P1123 取数游戏
    洛谷P2802 回家
    cf上分的失落->高兴->更失落
  • 原文地址:https://www.cnblogs.com/W--Jing/p/7977236.html
Copyright © 2011-2022 走看看