How to Read File Using Npoi

How to read file using NPOI

Simple read example below:

using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;

//.....

private void button1_Click(object sender, EventArgs e)
{
HSSFWorkbook hssfwb;
using (FileStream file = new FileStream(@"c:\test.xls", FileMode.Open, FileAccess.Read))
{
hssfwb= new HSSFWorkbook(file);
}

ISheet sheet = hssfwb.GetSheet("Arkusz1");
for (int row = 0; row <= sheet.LastRowNum; row++)
{
if (sheet.GetRow(row) != null) //null is when the row only contains empty cells
{
MessageBox.Show(string.Format("Row {0} = {1}", row, sheet.GetRow(row).GetCell(0).StringCellValue));
}
}
}

By the way: on NPOI website here in Download section there is example package - a pack of C# examples. Try it, if you haven't yet. :)

Read Image From Excel File Using NPOI

Okay, so I figured out a way to save the images locally.

var lst = originalWorkbook.GetAllPictures();
for (int i = 0; i < lst.Count; i++)
{
var pic = (XSSFPictureData) lst[i];
byte[] data = pic.Data;
BinaryWriter writer = new BinaryWriter(File.OpenWrite(String.Format("{0}.jpeg", i)));
writer.Write(data);
writer.Flush();
writer.Close();
}

Using the above code, I was able to successfully save all images locally as jpeg files.

However, if anyone knows a better or simpler way to copy image from one excel file to another, please help out and answer!



Related Topics



Leave a reply



Submit