Click here to Skip to main content
15,889,266 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi ,

When I convert excel to text with the help of excel saveAs method then extra double quotes come with string(text) data, for this

Problem after did google, we found one solution to use .prn(printer file) then issue resolved but it's printer file so data cut.full data not coming in printer file. I'm not found the proper solution so that.
C#
public static void GetExcelToText(string sFilePath)
        {
            Excelintrop.Application appExl = null;
            Excelintrop.Workbook workbook = null;
            Excelintrop.Worksheet nwSheet = null;
            List<string> filePaths = null;
            string temPath = sFilePath;
            try
            {
                filePaths=new List<string>();
                appExl = new Excelintrop.Application();
                appExl.DisplayAlerts = false;
                workbook = appExl.Workbooks.Open(sFilePath, Missing.Value, Missing.Value, Missing.Value,
                                                  Missing.Value, Missing.Value, Missing.Value, Missing.Value,
                                                  Missing.Value, Missing.Value, Missing.Value, Missing.Value,
                                                  Missing.Value, Missing.Value, Missing.Value);
                
                for (int i = 1; i <= workbook.Sheets.Count;i++)
                {
                    temPath = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location)+ Path.DirectorySeparatorChar + Guid.NewGuid().ToString() + ".prn";
                    nwSheet = (Excelintrop.Worksheet)workbook.Sheets[i];
                                       
                    Excelintrop.Range rang = nwSheet.UsedRange;
                    
                    rang.Cells.WrapText = true;
                    rang.Columns.AutoFit();

                    nwSheet.SaveAs(Filename: temPath, FileFormat: Excelintrop.XlFileFormat.xlTextPrinter, CreateBackup:false);                   
                    filePaths.Add(temPath);
                }
                workbook.Close();
                appExl.Workbooks.Close();
                appExl.Quit();

                using (var output = new StreamWriter(Path.ChangeExtension(sFilePath, ".txt")))
                {
                   foreach(var path in filePaths)
                    {
                        using (var input = new StreamReader(path))
                        {
                            output.WriteLine(input.ReadToEnd());
                        }
                    }
                }
              
            //File.Delete(Path.ChangeExtension(sFilePath, ".txt"));
            File.Move(Path.ChangeExtension(sFilePath, ".prn"), Path.ChangeExtension(sFilePath, ".txt"));
            File.Delete(Path.ChangeExtension(sFilePath, ".prn"));
            }            
            catch (Exception ex)
            {
                Log.Debug("ProLaw.Utils: OfficeExtensionFunctions: GetExcelToText" + ex.Message);
            }
            finally
            {
                foreach (var path in filePaths)
                {
                    File.Delete(path);
                }
                filePaths = null;
                File.Delete(Path.ChangeExtension(sFilePath, ".xls"));
                appExl.Workbooks.Close();
                appExl.Quit();
            }
        }


what is the best approach to get full data without double quotes?

Thanks,

Kapil

What I have tried:

I try the above-mentioned approach.
Posted
Updated 26-Sep-17 23:14pm
v2
Comments
Richard MacCutchan 27-Sep-17 5:21am    
Why are you saving as xlTextPrinter and then changing the extension from .prn to .txt? Use xlTextWindows to start with and you will not need all that redundant processing.
eddieangel 27-Sep-17 12:04pm    
I am firmly against using Excel interop for anything as I don't like the dependency on Microsoft Office, there are better solutions out there. That being said, have you tried stripping the double quotes from the input?

output.WriteLine(input.ReadToEnd().Replace("\"","");

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900