I'm starting to play with C# / .NET. My work background is Python.
Im having difficulty solving the conversion from Excel to TXT.
What i need the txt file to look like is:
ROW1COLUM1;ROW1COLUM2;ROW1COLUM3 //Note the lack of ";" on line break.
ROW2COLUM1;ROW2COLUM2;ROW3COLUM3
I am trying to at least save 1 cell in the txt but i cant. Code below:
private void button1_Click(object sender, EventArgs e) // Go
{
File.Create(@"C: \Users\AG\.PyCharmCE2017.2\config\scratches\testnet.txt").Close();
TextWriter tw = new StreamWriter(@"C: \Users\AG\.PyCharmCE2017.2\config\scratches\testnet.txt");
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(fileName);
Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
Excel.Range xlRange = xlWorksheet.UsedRange;
int rowCount = xlRange.Rows.Count;
int colCount = xlRange.Columns.Count;
string[] dataRow = new string[15]; //fixed number for now
for (int i = 1; i <= rowCount; i++)
{
for (int j = 1; j <= colCount; j++)
{
dataRow[j - 1] = xlWorksheet.Cells[i, j].Value.ToString();
}
tw.WriteLine(dataRow[1]); // Just checking if i can write something
}
MessageBox.Show("OK");
tw.Close();
GC.Collect();
GC.WaitForPendingFinalizers();
Marshal.ReleaseComObject(xlRange);
Marshal.ReleaseComObject(xlWorksheet);
xlWorkbook.Close();
Marshal.ReleaseComObject(xlWorkbook);
xlApp.Quit();
Marshal.ReleaseComObject(xlApp);
}
I also need to save the .txt file as unicode, in case that matters. This is probably very basic, i just couldn't find an answer. Thanks
EDIT:
I managed to make it work. I also added a backgroundWorker. The problem i have is performance. Can someone point me in the right direction?
Code:
if (goNoGo)
{
string sourceDirectory = Path.GetDirectoryName(fileName);
string filenameWithoutExtension = Path.GetFileNameWithoutExtension(fileName);
File.Create(sourceDirectory + filenameWithoutExtension).Close();
TextWriter tw = new StreamWriter(sourceDirectory + "\\" + filenameWithoutExtension + ".txt", true, Encoding.Unicode);
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(fileName);
Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
Excel.Range xlRange = xlWorksheet.UsedRange;
int rowCount = xlRange.Rows.Count;
int colCount = xlRange.Columns.Count;
string dataRow = "";
int z = 1;
if (checkBox1.Checked)
{
z = 2;
}
int cont = 0;
for (float i = z; i <= rowCount; i++)
{
if (i % 250 == 0) // every 250 rows, check BW updates
{
cont = ((int)((i / rowCount) * 100));
backgroundWorker1.ReportProgress(cont);
//MessageBox.Show(cont.ToString());
if (backgroundWorker1.CancellationPending)
{
e.Cancel = true;
backgroundWorker1.ReportProgress(0);
return;
}
}
for (int j = 1; j <= colCount; j++)
{
try
{
if (j == 1)
{
dataRow = xlWorksheet.Cells[i, j].Value.ToString();
}
else
{
dataRow += ";";
dataRow += xlWorksheet.Cells[i, j].Value.ToString();
}
}
catch (Exception ex) // catches empty cells
{
if (j == 1)
{
dataRpw = "";
}
else
{
dataRow += ";";
dataRow += "";
}
continue;
}
}
tw.WriteLine(dataRow);
}
tw.Close();
//cleanup
GC.Collect();
GC.WaitForPendingFinalizers();
Marshal.ReleaseComObject(xlRange);
Marshal.ReleaseComObject(xlWorksheet);
xlWorkbook.Close();
Marshal.ReleaseComObject(xlWorkbook);
xlApp.Quit();
Marshal.ReleaseComObject(xlApp);
backgroundWorker1.ReportProgress(100);
}
else if (extensionWrong)
{
MessageBox.Show("File must be .xls");
}
else
{
MessageBox.Show("Load a file");
}
A 30k row file can take up to an hour. Any ideas?