Home » excel » c# – Text to column in Excel

c# – Text to column in Excel

Posted by: admin April 23, 2020 Leave a comment

Questions:

I have an excel sheet containing single column and multiple rows like:

000500114673HY340216-00       CT TAPING WORK DTYPE-OSKF 245 TWB.T H/C 37990110000250000100

I am trying to create a new Excel file through my C# code, but not getting the desired result. In my new Excel file I need six columns with each column assigned a particular range, say for first column 12, for second 20 and so on (spaces also included). I used the text to column method, but I’m not able to parse it to fixed width. My code sample is:

xlAppNew = new Application();
xlAppNew.DisplayAlerts = true;
workbooks = xlAppNew.Workbooks;                   
workbook = workbooks.Open(@filepath, 0, true, 1, "", "", true,
           Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t",
           false, false, 0, true, 1, 0);
xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.get_Item(1);        
((Range)xlAppNew.Cells[1, 1]).EntireColumn.TextToColumns(Type.Missing,
           Microsoft.Office.Interop.Excel.XlTextParsingType.xlFixedWidth,
           Microsoft.Office.Interop.Excel.XlTextQualifier.xlTextQualifierNone,
           Type.Missing, Type.Missing, Type.Missing, true, Type.Missing,
           Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
           Type.Missing);

I think the problem lies with the fourth last parameter, the fieldinfo object which I am not able to provide properly, I also used the array object, but it produced error. I took help from this question 4945621. But the exact solution is not provided there.

How to&Answers:

Actually what I had to do was first convert a DAT file to Excel and then divide it into fixed number of columns through my C# code. So in my first step what I did was converted the DAT file into an Excel file having a single column and given number of rows as my temporary Excel file.

                xlAppNew1 = new Application();
                xlAppNew1.DisplayAlerts = true;
                workbooks1 = xlAppNew1.Workbooks;
                workbook1 = workbooks1.Open(@filepath, 0, true, 1, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
                xlAppNew1.ActiveWorkbook.SaveAs(@filepathNew, -4143, "", "", false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
                missing, missing, missing, missing, missing);

Now make sure to close all the instances of the COM object that you have created otherwise it will create hell lot of a problem.Secondly in another function I opened my temporary Excel file, and all the data in a Dataset and then with the help of Range object and the following method:

     range = range.get_Resize(range.Rows.Count, 9);           
     string[,] saRet = new string[range.Rows.Count, 9];         
     range.set_Value(Missing.Value, saRet);

The whole code goes something loke this:

                xlAppNew = new Application();
                xlAppNew.DisplayAlerts = true;
                workbooks = xlAppNew.Workbooks;
                workbook = workbooks.Open(@filepathnew, 0, true, 1, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
                xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.get_Item(1);                    
                if (getExtension.ToLower() == ".xls")
                    connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath_second + ";Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1;\"";

                else
                    connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filepath_second + ";Extended Properties=Excel 12.0 Xml;HDR=Yes;IMEX=1;";

                OleDbConnection con = new OleDbConnection(connString);

                con.Open();
                System.Data.DataTable dtSheet = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                string tname = dtSheet.Rows[0]["TABLE_NAME"].ToString();                    
                OleDbDataAdapter ad = new OleDbDataAdapter(@"Select * FROM [" + tname + "];", con);
                DataSet dset = new DataSet();
                ad.Fill(dset);
                range = xlWorkSheet.UsedRange;
                range = range.get_Resize(range.Rows.Count, 9);
                //Create an array.
                string[,] saRet = new string[range.Rows.Count, 9];
                //Fill the array.
                for (int iRow = 0; iRow < range.Rows.Count - 1; iRow++)
                {
                    for (int iCol = 0; iCol < 9; iCol++)
                    {
                        switch (iCol)
                        {
                            case 0:
                                saRet[iRow, iCol] = dset.Tables[0].Rows[iRow][0].ToString().Substring(3, 9);//iRow.ToString() + "|" + iCol.ToString();
                                break;
                            case 1:
                                saRet[iRow, iCol] = dset.Tables[0].Rows[iRow][0].ToString().Substring(12, 4);
                                break;
                            case 2:
                                saRet[iRow, iCol] = dset.Tables[0].Rows[iRow][0].ToString().Substring(16, 18);
                                break;
                            case 3:
                                saRet[iRow, iCol] = dset.Tables[0].Rows[iRow][0].ToString().Substring(34, 8);
                                break;
                            case 4:
                                saRet[iRow, iCol] = dset.Tables[0].Rows[iRow][0].ToString().Substring(42, 4);
                                break;
                            case 5:
                                saRet[iRow, iCol] = dset.Tables[0].Rows[iRow][0].ToString().Substring(46, 18);
                                break;
                            case 6:
                                saRet[iRow, iCol] = dset.Tables[0].Rows[iRow][0].ToString().Substring(64, 40);
                                break;
                            case 7:
                                saRet[iRow, iCol] = dset.Tables[0].Rows[iRow][0].ToString().Substring(104, 3);
                                break;
                            case 8:
                                saRet[iRow, iCol] = dset.Tables[0].Rows[iRow][0].ToString().Substring(107, 5);
                                break;

                        }

                    }
                }

                //Set the range value to the array.
                range.set_Value(Missing.Value, saRet);
                FieldInfo myf = typeof(MyNewService).GetField("saRet");//Fieldinfo object is also an important part
                //**********************
                ((Range)xlAppNew.Cells[1, 1]).EntireColumn.TextToColumns(Type.Missing, Microsoft.Office.Interop.Excel.XlTextParsingType.xlFixedWidth, Microsoft.Office.Interop.Excel.XlTextQualifier.xlTextQualifierNone, Type.Missing, Type.Missing, Type.Missing, true, Type.Missing, Type.Missing, Type.Missing, myf, Type.Missing, Type.Missing, Type.Missing);
                xlAppNew.ActiveWorkbook.SaveAs(@temp_path, -4143, "", "", false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, missing, missing, missing, missing, missing);

Hope it helps others to save their time.Thank You.