We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Import fixed width file into workbook

Hi


I would like to import the attached fixed width field documetn into a Workbook.

//Get the Excel file into stream
FileStream stream = new FileStream(@“D:\data\1\nat00010.txt”, FileMode.Open);
  <span style="color:green;">//Instantiate LoadOptions specified by the LoadFormat.</span>
  <span style="color:#2b91af;">LoadOptions</span> loadOptions = <span style="color:blue;">new</span> <span style="color:#2b91af;">LoadOptions</span>(<span style="color:#2b91af;">LoadFormat</span>.Auto);


  <span style="color:#2b91af;">Workbook</span> workbook = <span style="color:blue;">new</span> <span style="color:#2b91af;">Workbook</span>(stream, loadOptions);</pre><pre style="font-family: Consolas; font-size: 13px; background: rgb(250, 240, 222);">When i try to run this code I gets an error _message=Cannot read that as a ZipFile</pre><pre style="font-family: Consolas; font-size: 13px; background: rgb(250, 240, 222);"><br></pre><pre style="font-family: Consolas; font-size: 13px; background: rgb(250, 240, 222);">Regards,</pre><pre style="font-family: Consolas; font-size: 13px; background: rgb(250, 240, 222);">Joe</pre></div>

Hi Joe,

Thanks for your posting and using Aspose.Cells.

In order to load a text file, we need to know the separator and separator should be single character. In your text file, there is no separator.

I downloaded and used the latest version: Aspose.Cells
for .NET v8.2.0.2
.

Suppose the separator in your text file is comma, then you will load your text file with the following code. I have attached the output xlsx file generated by this code for your reference.

C#


string filePath = @“F:\Shak-Data-RW\Downloads\nat00010.txt”;


TxtLoadOptions options = new TxtLoadOptions();

options.Separator = ‘,’; //suppose separator is comma


Workbook workbook = new Workbook(filePath, options);

workbook.Save(“output.xlsx”);

Hi Shakeel


The file is fixed width for every field, that is the delimiter. It sounds like aspose cells cant import this kind of file is that correct?

Regards,
Joe

Hi Joe,

Thanks for your posting and using Aspose.Cells.

You are right, Aspose.Cells cannot import this fixed width file. You need to first do some extra processing and convert it to comma separated (or some other delimiter separated) file, then import it in workbook object using Aspose.Cells API.