using System;
using ;
using ;
using Excel = ;
namespace ExportExcelToCode
{
public partial class ExcelUtils
{
/// <summary>
/// Get the Sheet name
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
public static string[] GetSheetNameList(string filePath)
{
try
{
string connectionText = "Provider=.12.0;" + "Data Source=" + filePath + ";" + "Extended Properties='Excel 12.0;HDR=YES;IMEX=1';";
oleDbConnection = new (connectionText);
();
dataTable = (, new object[] { null, null, null, "TABLE" }); ;
string[] sheetNameList = new string[];
for (int index = 0; index < ; index++)
{
sheetNameList[index] = [index][2].ToString();
}
();
return sheetNameList;
}
catch (Exception ex)
{
return null;
}
}
/// <summary>
/// Excel to DataTable
/// </summary>
/// <param name="filePath"></param>
/// <param name="sheetName"></param>
/// <returns></returns>
public static ExcelToDataTable(string filePath, string sheetName)
{
try
{
string connectionText = "Provider=.12.0;" + "Data Source=" + filePath + ";" + "Extended Properties='Excel 12.0;HDR=YES;IMEX=1';";
string selectText = ("select * from [{0}$]", sheetName);
DataSet dataSet = new DataSet();
oleDbConnection = new (connectionText);
();
oleDbDataAdapter = new (selectText, connectionText);
(dataSet, sheetName);
();
return [sheetName];
}
catch (Exception ex)
{
return null;
}
}
/// <summary>
/// Excel to Csv
/// </summary>
/// <param name="sourceExcelPathAndName"></param>
/// <param name="targetCSVPathAndName"></param>
/// <param name="excelSheetName"></param>
/// <param name="columnDelimeter"></param>
/// <param name="headerRowsToSkip"></param>
/// <returns></returns>
public static bool ExcelToCsv(string sourceExcelPathAndName, string targetCSVPathAndName, string excelSheetName, string columnDelimeter, int headerRowsToSkip)
{
oXL = null;
workbooks = null;
Workbook mWorkBook = null;
Sheets mWorkSheets = null;
Worksheet mWSheet = null;
try
{
oXL = new ();
= false;
= false;
workbooks = ;
mWorkBook = (sourceExcelPathAndName, 0, false, 5, "", "", false, , "", true, false, 0, true, false, false);
mWorkSheets = ;
mWSheet = (Worksheet)mWorkSheets.get_Item(excelSheetName);
range = ;
rngCurrentRow;
for (int i = 0; i < headerRowsToSkip; i++)
{
rngCurrentRow = range.get_Range("A1", ).EntireRow;
();
}
("\n", " ", , , , , , );
(",", columnDelimeter, , , , , , );
(targetCSVPathAndName, ,
, , , , ,
, , ,
, false);
return true;
}
catch (Exception ex)
{
return false;
}
finally
{
if (mWSheet != null) mWSheet = null;
if (mWorkBook != null) (, , );
if (mWorkBook != null) mWorkBook = null;
if (oXL != null) ();
(oXL);
if (oXL != null) oXL = null;
();
();
();
();
}
}
}
}