在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
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(); } } } }
excelApplication = new Excel.ApplicationClass(); 导致整个程序无法运行,所以我稍作修改成我以上所写的代码,运行调试后,暂无发现明显问题。 |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论