C # Use NPOI to obtain the image in Excel and save it to a local algorithm,

Source: Internet
Author: User
Tags image png

C # Use NPOI to obtain the image in Excel and save it to a local algorithm,

Requirements:Read the image in excel and save it to the specified path.

Idea: Use the GetAllPictures () method in NPOI to obtain image information

Steps:

1. Create a Windows form application

2. Create an excel file on the desktop and paste two images to it.

For example:

 

3. Drag a button into Form.

4. click the button and write it in the Click Event method. The method to read the image is ExcelToImage.

The method for clicking an event is as follows:

Private string exclePath = @ "C: \ users \ lenovo \ Desktop \ testPic.xls"; private void button2_Click (object sender, EventArgs e) {List <string> listpath = new List <string >{}; string savePath = Path. combine ("E: \", "pic"); if (! Directory. exists (savePath) // determines whether a folder is saved. If no folder Exists, create a Directory. createDirectory (savePath); bool result = ExcelToImage (exclePath, savePath, ref listpath); if (result) MessageBox. show ("exported"); else MessageBox. show ("Export failed ");}

 

5. The ExcelToImage method event is as follows:

/// <Summary> /// obtain the image from excel /// </summary> /// <param name = "filepath"> file path </param> /// <param name = "savepath"> image storage path </param> // <param name = "listPath"> returns the saved chart address list </param> // <returns> whether the image is saved successfully </returns> private bool ExcelToImage (string filepath, string savepath, ref List <string> listPath) {try {using (FileStream fsReader = File. openRead (filepath) {HSSFWorkbook wk = new HSSFWorkbook (fsReader ); IList pictures = wk. getAllPictures (); int I = 0; foreach (HSSFPictureData pic in pictures) {// if (pic. data. length = 19504) // skip the image that does not need to be saved, where pic. data has image length // continue; string ext = pic. suggestFileExtension (); // obtain the extension string path = string. empty; if (ext. equals ("jpg") {Image jpg = Image. fromStream (new MemoryStream (pic. data); // from pic. data flow creates an image path = Path. combine (savepath, string. format ("picture0).jpg ", I ++); jpg. save (path); // Save} else if (ext. equals ("png") {Image png = Image. fromStream (new MemoryStream (pic. data); path = Path. combine (savepath, string. format ("pic{02.16.png", I ++); png. save (path);} if (! String. IsNullOrEmpty (path) listPath. Add (path) ;}} catch (Exception ex) {return false;} return true ;}

 

Result:

 

Note:In the HSSFWorkbook class of this algorithm, the corresponding excelite should be (the extension name is. xls.

HSSFWorkbook: indicates the operation of excel20031. the extension name is .xls.

XSSFWorkbook: it is the operation of the Excel2007 region, the extension name is .xlsx


Copyright Disclaimer: This article is an original article by the blogger. For more information, see the source. Http://www.cnblogs.com/SweetMemory/p/6378775.html

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.