/ Published in: C#
Convert Excel file (XLS) to CSV
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
using System; using System.Data; using Microsoft.SqlServer.Dts.Runtime; using System.Windows.Forms; using Excel = Microsoft.Office.Interop.Excel; using Microsoft.Office.Interop.Excel; namespace ST_6dc747ff29cf41c6ac11b7c0bca33d19.csproj { [System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")] public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase { #region VSTA generated code enum ScriptResults { Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success, Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure }; #endregion /* The execution engine calls this method when the task executes. To access the object model, use the Dts property. Connections, variables, events, and logging features are available as members of the Dts property as shown in the following examples. To reference a variable, call Dts.Variables["MyCaseSensitiveVariableName"].Value; To post a log entry, call Dts.Log("This is my log text", 999, null); To fire an event, call Dts.Events.FireInformation(99, "test", "hit the help message", "", 0, true); To use the connections collection use something like the following: ConnectionManager cm = Dts.Connections.Add("OLEDB"); cm.ConnectionString = "Data Source=localhost;Initial Catalog=AdventureWorks;Provider=SQLNCLI10;Integrated Security=SSPI;Auto Translate=False;"; Before returning from this method, set the value of Dts.TaskResult to indicate success or failure. To open Help, press F1. */ private static Workbook mWorkBook; private static Sheets mWorkSheets; private static Worksheet mWSheet1; private static Excel.Application oXL; private static string ErrorMessage = string.Empty; public void Main() { try { string sourceExcelPathAndName = @"D:\Excel Import\Excel Import.xls"; string targetCSVPathAndName = @"D:\Excel Import\Excel Import.csv"; string excelSheetName = @"Sheet1"; string columnDelimeter = @"|#|"; int headerRowsToSkip = 0; if (ConvertExcelToCSV(sourceExcelPathAndName, targetCSVPathAndName, excelSheetName, columnDelimeter, headerRowsToSkip) == true) { Dts.TaskResult = (int)ScriptResults.Success; } else { Dts.TaskResult = (int)ScriptResults.Failure; } } catch (Exception ex) { Dts.TaskResult = (int)ScriptResults.Failure; } } public static bool ConvertExcelToCSV(string sourceExcelPathAndName, string targetCSVPathAndName, string excelSheetName, string columnDelimeter, int headerRowsToSkip) { try { oXL.Visible = false; oXL.DisplayAlerts = false; Excel.Workbooks workbooks = oXL.Workbooks; mWorkBook = workbooks.Open(sourceExcelPathAndName, 0, false, 5, "", "", false, XlPlatform.xlWindows, "", true, false, 0, true, false, false); //Get all the sheets in the workbook mWorkSheets = mWorkBook.Worksheets; //Get the specified sheet mWSheet1 = (Worksheet)mWorkSheets.get_Item(excelSheetName); Excel.Range range = mWSheet1.UsedRange; //deleting the specified number of rows from the top Excel.Range rngCurrentRow; for (int i = 0; i < headerRowsToSkip; i++) { rngCurrentRow = range.get_Range("A1", Type.Missing).EntireRow; rngCurrentRow.Delete(XlDeleteShiftDirection.xlShiftUp); } //replacing ENTER with a space range.Replace("\n", " ", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); //replacing COMMA with the column delimeter range.Replace(",", columnDelimeter, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); mWorkBook.SaveAs(targetCSVPathAndName, XlFileFormat.xlCSVMSDOS, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, false); return true; } catch (Exception ex) { ErrorMessage = ex.ToString(); return false; } finally { if (mWSheet1 != null) mWSheet1 = null; if (mWorkBook != null) mWorkBook.Close(Type.Missing, Type.Missing, Type.Missing); if (mWorkBook != null) mWorkBook = null; if (oXL != null) oXL.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(oXL); if (oXL != null) oXL = null; GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); } } } }
URL: XLStoCSV