c#实现将Excel文件导出为csv和UTF8格式的txt文件
生活随笔
收集整理的這篇文章主要介紹了
c#实现将Excel文件导出为csv和UTF8格式的txt文件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
工作中為了方便,經常使用excel表進行編輯,然后轉換為程序用的csv和txt文件。
用c#做了個小工具,生成的exe文件放到需要轉換的excel文件目錄,可以實在自動讀取該目錄下的所有xlsx文件,并在該目錄下創建csv和txt兩個文件夾,用來存放轉換之后的csv和txt文件。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data.OleDb; //添加引用--》引用管理器--》COM類型庫中找到Microsoft Excel xx Object Library //xx看自己的excel版本 using Microsoft.Office.Interop.Excel; using System.IO; using System.Reflection; using System.Diagnostics;//功能目標,將當前目錄下的所有xlsx的EXCEL文件導出為csv和txt格式 namespace ExcelTools {class Program{private static void QuitExcel(){Process[] excels = Process.GetProcessesByName("EXCEL");foreach (var item in excels){item.Kill();}}static void Main(string[] args){QuitExcel();string InputFilePath=System.Environment.CurrentDirectory; //excel文件目錄string OutputFilePath_csv = InputFilePath+@"\csv";//excel導出csv目錄string OutputFilePath_txt = InputFilePath+@"\txt";//excel導出txt目錄string FileContent = string.Empty;//臨時存儲文件內容,讀取excel內容,存放到csv和txt文件//創建導出csv和txt文件的目錄if (Directory.Exists(OutputFilePath_csv)){Console.WriteLine(OutputFilePath_csv+"存在了");Directory.Delete(OutputFilePath_csv,true);Directory.CreateDirectory(OutputFilePath_csv);}else{Directory.CreateDirectory(OutputFilePath_csv);}if (Directory.Exists(OutputFilePath_txt)){Console.WriteLine(OutputFilePath_txt + "存在了");Directory.Delete(OutputFilePath_txt, true);Directory.CreateDirectory(OutputFilePath_txt);}else{Directory.CreateDirectory(OutputFilePath_txt);}#region 在當前目錄創建excel測試用文件創建文件名// string[] s = new string[10];// for (int i = 0; i < 10; i++)// {// s[i] = InputFilePath + @"\" + "test" + i + ".xlsx";// }// //檢查目錄下是否存在這些文件,如果存在則刪除// foreach (var item in s)// {// if (Directory.Exists(item))// {// Console.WriteLine(item);// }// else// {// File.Delete(item);// }// }// //在當前目錄創建excel測試文件// object Nothing = Missing.Value;// Application excelApp = new Application();// excelApp.Visible = false;// foreach (var item in s)// {// Workbook excelDoc = excelApp.Workbooks.Add(Nothing);// Worksheet excelSheet = excelDoc.Sheets[1];// excelSheet.Cells[1, 1] = "ID";// excelSheet.Cells[1, 2] = "Name";// excelSheet.Cells[1, 3] = "IP";// excelSheet.Cells[1, 4] = "Desc";// excelSheet.SaveAs(item, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);// excelDoc.Close(false, Type.Missing, Type.Missing);// }// excelApp.Quit();#endregion#region 讀取當前目錄下的所有xlsx文件//獲取應用程序運行目錄DirectoryInfo dir = new DirectoryInfo(InputFilePath);FileInfo[] filename = dir.GetFiles("*.xlsx");Application App = new Application();object nothing = Missing.Value;foreach (var item in filename){string newFileNameCSV = string.Empty;//保存到csvWorkbook AppBook = App.Workbooks.Open(item.FullName, nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing);Worksheet AppSheet = AppBook.Worksheets[1];newFileNameCSV = OutputFilePath_csv + @"\" + item.Name.Replace("xlsx", "csv");AppSheet.SaveAs(newFileNameCSV, XlFileFormat.xlCSV, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);AppBook.Close(false, Type.Missing, Type.Missing);AppBook = null;Console.WriteLine(item);}foreach (var item in filename){string newFileNameTXT = string.Empty;//保存到txtWorkbook AppBook = App.Workbooks.Open(item.FullName, nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing);Worksheet AppSheet = AppBook.Worksheets[1];newFileNameTXT = OutputFilePath_txt + @"\" + item.Name.Replace("xlsx", "txt");Console.WriteLine(newFileNameTXT);AppBook.SaveAs(newFileNameTXT, XlFileFormat.xlCSV, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing);AppBook.Close(false, Type.Missing, Type.Missing);AppBook = null;//將txt文件的編碼格式修改為UTF8StreamReader sr = new StreamReader(newFileNameTXT, Encoding.Default, false);string contenttxt = sr.ReadToEnd();sr.Close();StreamWriter sw = new StreamWriter(newFileNameTXT, false, Encoding.UTF8);sw.Write(contenttxt);sw.Close();//Console.WriteLine(contenttxt);Console.WriteLine(newFileNameTXT);}App.Quit();#endregionConsole.ReadKey();}} }總結
以上是生活随笔為你收集整理的c#实现将Excel文件导出为csv和UTF8格式的txt文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 问题 C: Cly的三角形
- 下一篇: 计算机桌面图标的使用,电脑桌面图标不见了