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.
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.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.