1

I try to read an RTF file and send text for this to Excel file. I do this:

Byte[] rtf;
using (FileStream stream = new FileStream(Server.MapPath("/1.RTF"), 
    FileMode.Open, FileAccess.Read))
{
    int size = Convert.ToInt32(stream.Length);
    rtf = new Byte[size];
    stream.Read(rtf, 0, size);
};

textx = System.Text.Encoding.GetEncoding("utf-8").GetString(rtf);

string file = @"C:\Users\ITS_prog\Desktop\newdoc.xls";
Workbook workbook = Workbook.Load(file);
Worksheet worksheet = workbook.Worksheets[1];
worksheet.Cells[0, 1] = new Cell((short)1);
worksheet.Cells[2, 0] = new Cell(9999999);
worksheet.Cells[3, 3] = new Cell((decimal)3.45);
worksheet.Cells[2, 2] = new Cell(textx);
worksheet.Cells[2, 4] = new Cell("Second string");
worksheet.Cells[4, 0] = new Cell(32764.5, "#,##0.00");
worksheet.Cells[5, 1] = new Cell(DateTime.Now, @"YYYY-MM-DD");
worksheet.Cells.ColumnWidth[0, 1] = 3000;
workbook.Save(file);

But in this row, Workbook workbook = Workbook.Load(file); my app give the exception:

Exception unable to read beyond the end of the stream.

What did I do wrong?

Axel Kemper
  • 10,544
  • 2
  • 31
  • 54
  • @AxelKemper I'm using ExcelLibrary. I can't use Excel Interop, I haven't excel on server – Alexandr Docker Dec 12 '17 at 19:02
  • Can you open the file in question with `Excel` (or some other program capable to read `Excel` files) to show that the file is consistent? – Axel Kemper Dec 12 '17 at 19:10
  • @AxelKemper it is empty. It have only two empty sheets. If i didn't read rtf file, I would write data to excel. – Alexandr Docker Dec 12 '17 at 19:13
  • Can't you just read the file with `string text = File.ReadAllText(path, encoding)`? – Olivier Jacot-Descombes Dec 12 '17 at 19:20
  • To create an empty `Workbook` ready to receive your data, just use `Workbook workbook = new Workbook();`. After filling in the data, you `save` it to a file as demonstrated [here](https://stackoverflow.com/questions/8928740/excellibrary-documentation) – Axel Kemper Dec 12 '17 at 19:22
  • @AxelKemper I did it `Workbook workbook = new Workbook(); workbook = Workbook.Load(file);` but it doesn't work too – Alexandr Docker Dec 12 '17 at 19:27
  • @OlivierJacot-Descombes I tried did it: `string rtfText = System.IO.File.ReadAllText(Server.MapPath("/1.RTF"), System.Text.Encoding.UTF8); Workbook workbook = new Workbook(); workbook = Workbook.Load(file);` but it doesn't work too – Alexandr Docker Dec 12 '17 at 19:30
  • You cannot load an empty (zero bytes) file. I don't understand what you are trying to achieve. How do you intend to interprete the RTF contents? Creating the empty Workbook and writing it to file should work. – Axel Kemper Dec 12 '17 at 19:33
  • @AxelKemper I must add infromation from rtf file, to existent excel file( If I can do new file of course I do it. Excel file it is not empty. – Alexandr Docker Dec 12 '17 at 19:35
  • RTF files are text files with formatting information. To read RTF, you are going to need a suitable tool or library. Otherwise, you cannot extract data from RTF files. Excellibrary is only capable of reading/writing Excel files, not RTF files. – Axel Kemper Dec 12 '17 at 19:40
  • can You advise me some free library for decision this task? – Alexandr Docker Dec 12 '17 at 19:43

0 Answers0