Acces to file. Can't write to file

Jul 20, 2011 at 11:09 AM

Hello this is my example.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using NPOI.HSSF.UserModel;
using NPOI.HPSF;
using NPOI.POIFS.FileSystem;
using NPOI.HSSF.Util;
using NPOI.SS.UserModel;

namespace ConsoleApplication2
{
    class Program
    {
       
        static void Main(string[] args)
        {
            FileStream fs = new FileStream(("testtest1lg.xls"), FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
            HSSFWorkbook templateWorkbook = new HSSFWorkbook(fs);
            Sheet sheet1 = templateWorkbook.GetSheet("a");
            sheet1.CreateRow(5).CreateCell(4).SetCellValue(4);
           
            templateWorkbook.Write(fs);
            fs.Close();
        }
    }
}

 

I would like open the file write some date and close the file but when I compile the code i heave a error: Cannot access a closed file.Please help me how I can get access to file .

Coordinator
Oct 16, 2011 at 10:32 PM

You should create another stream object to write the file. Otherwise, after it's used by HSSFWorkbook, it's closed.