It is my 2nd time dealing with NPOI. I'm creating a workbook from a template excel file like this.
string dldir = AppDomain.CurrentDomain.BaseDirectory + "Download\\excel\\";
string uldir = AppDomain.CurrentDomain.BaseDirectory + "Upload\\default_export_file\\";
string filename = DateTime.Now.ToString("yyyy-M-d") + "_" + user.first_name + "_" + query.default_export_filename;
System.IO.File.Delete(Path.Combine(dldir, filename));
//System.IO.File.Copy(Path.Combine(uldir, query.default_export_filename), Path.Combine(dldir, filename));
HSSFWorkbook hssfwb;
using (FileStream file = new FileStream(Path.Combine(uldir, query.default_export_filename), FileMode.Open, FileAccess.ReadWrite))
{
hssfwb = new HSSFWorkbook(file);
}
MemoryStream mstream = new MemoryStream();
hssfwb.Write(mstream);
FileStream xfile = new FileStream(Path.Combine(dldir, filename), FileMode.Create, System.IO.FileAccess.Write);
byte[] bytes = new byte[mstream.Length];
mstream.Read(bytes, 0, (int)mstream.Length);
xfile.Write(bytes, 0, bytes.Length);
xfile.Close();
mstream.Close();
But when I checked the created file. It contains nothing. The size is correct. But the contents is just blanks. And I couldn't open it in MS Excel either. I repeated and traced the code but I can't seem to find what is causing it to just write blanks.
Anyone knows why is this happening
FYI: I'm using VS 2010, ASP.Net MVC 4 and NPOI 1.2.5 installed from the NuGet Gallery
How about this:
MemoryStream mstream = new MemoryStream();
hssfwb.Write(mstream);
FileStream xfile = new FileStream(Path.Combine(dldir, filename), FileMode.Create, System.IO.FileAccess.Write);
byte[] bytes = new byte[mstream.Length];
mstream.Read(bytes, 0, (int)mstream.Length);
xfile.Write(bytes, 0, bytes.Length);
xfile.Close();
mstream.Close();
Modified:
FileStream xfile = new FileStream(Path.Combine(dldir, filename), FileMode.Create, System.IO.FileAccess.Write);
hssfwb.Write(xfile);
xfile.Close();