I am building an application in Visual Studio 2019 Community edition to convert an Excel XLS file to a CSV. I had found some example code that was using the ExcelReader library and modified it so it would pull the XLS file and make a test.csv file. When I run this I do not get any errors back, but the file is not created.
This is to help automate a currently manual process where we have to manually open the XLS file and save it as a CSV. I am using Visual Studio Community edition (2019) and have added the ExcelReader library.
'''public partial class Default2 : System.Web.UI.Page
{
static void Main() { }
DataSet result = new DataSet();
string filePath =
@"C:\Users\BEM26331\Documents\AppDevProjects\9.10.19.xls";
protected void UploadButton_Click(object sender, EventArgs e)
{
}
protected void Page_Load(object sender, EventArgs e)
{
FileStream stream = File.Open(filePath, FileMode.Open,
FileAccess.Read);
Excel.IExcelDataReader excelReader =
Excel.ExcelReaderFactory.CreateBinaryReader(stream);
DataSet result = excelReader.AsDataSet();
excelReader.Close();
result.Tables[0].TableName.ToString();
string csvData = "";
int row_no = 0;
int ind = 0;
while (row_no < result.Tables[ind].Rows.Count)
{
for (int i = 0; i < result.Tables[ind].Columns.Count; i++)
{
csvData += result.Tables[ind].Rows[row_no][i].ToString()
+ ",";
}
row_no++;
csvData += "\n";
}
string output =
@"C:\Users\BEM26331\Documents\AppDevProjects\test.csv";
StreamWriter csv = new StreamWriter(@output, false);
csv.Write(csvData);
csv.Close();
}
}'''
The end result should create the same file, but saved as a CSV. The actual output does not create any file.
Something like this perhaps?: