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
Mysqlcommand Command.Parameters.Add Is Obsolete
How to Select Text from the Richtextbox and Then Color It
Internet Explorer Protective Mode Setting and Zoom Levels
What Is the Worst Gotcha in C# or .Net
Are There Any Suggestions for Developing a C# Coding Standards/Best Practices Document
How to Read Values from the Querystring with ASP.NET Core
How to Get Values from Igrouping
Correct Way Communicate Wsse Usernametoken for Soap Webservice
Determine If Uploaded File Is Image (Any Format) on MVC
Verifying That a String Contains Only Letters in C#
Linq .Any VS .Exists - What's the Difference
Convert.Changetype() Fails on Nullable Types
Hashing Passwords with Md5 or Sha-256 C#
Validation Error Style in Wpf, Similar to Silverlight
How to Get the Name of the Current Executable in C#