SHARE
TWEET

Untitled

a guest Jul 16th, 2019 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.         public static void ConvertToCSV(string fileToConvertPath, string convertedFilePath = null)
  2.         {
  3.             System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); // NĂ©cessaire pour la prise en charge de certains types d'encodage de fichier.
  4.  
  5.             FileStream fileToConvertStream = File.Open(fileToConvertPath, FileMode.Open, FileAccess.Read, FileShare.None);
  6.             IExcelDataReader excelReader = ExcelReaderFactory.CreateReader(fileToConvertStream);
  7.             DataSet xlsData = excelReader.AsDataSet();
  8.             excelReader.Close();
  9.  
  10.             string csvData = "";
  11.             for (int row = 0; row < xlsData.Tables[0].Rows.Count; row++)
  12.             {
  13.                 for (int column = 0; column < xlsData.Tables[0].Columns.Count; column++)
  14.                     csvData += xlsData.Tables[0].Rows[row][column].ToString() + ";";
  15.                 csvData += "\n";
  16.             }
  17.  
  18.             StreamWriter convertedDataStream = new StreamWriter(
  19.                 string.IsNullOrWhiteSpace(convertedFilePath)
  20.                     ? Path.GetDirectoryName(fileToConvertPath) + "\\" + Path.GetFileNameWithoutExtension(fileToConvertPath) + ".csv"
  21.                     : convertedFilePath,
  22.                 false,
  23.                 Encoding.UTF8
  24.             );
  25.             convertedDataStream.Write(csvData);
  26.             convertedDataStream.Close();
  27.         }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top