Npoi export Excel files with images and controllable sizes

Source: Internet
Author: User

Using npoi. hssf. usermodel;
Using npoi. hssf. util;
Using npoi. DDF;
Using npoi. Ss. usermodel;
Using system. IO;
Using npoi. SS;
# Region Export
Protected void btnexcel_click (Object sender, eventargs E)
{
Qqt_bll.gg_content BLL = new qqt_bll.gg_content ();
Dataset DS = BLL. getds ("");
Datatable dt = Ds. Tables [0];
String excelname = system. datetime. now. tostring (). replace (":",""). replace ("-",""). replace ("","");
String filepath = system. Web. httpcontext. Current. server. mappath ("readexcel") + "\" + excelname + ". xls ";
Memorystream MS = renderableabletoexcel (DT) as memorystream;
Filestream FS = new filestream (filepath, filemode. Create, fileaccess. Write );
Byte [] DATA = Ms. toarray ();
FS. Write (data, 0, Data. Length );
FS. Flush ();
FS. Close ();
Data = NULL;
MS = NULL;
FS = NULL;

# Export region to the client
Response. contentencoding = system. Text. encoding. getencoding ("gb2312 ");
Response. appendheader ("content-disposition", "attachment; filename =" + system. web. httputility. urlencode (excelname, system. text. encoding. utf8) + ". xls ");
Response. contenttype = "application/Excel ";
Response. writefile (filepath );
Response. End ();
# Endregion
}
Public stream renderableabletoexcel (sourable sourcetable)
{

Memorystream MS = new memorystream ();
Npoi. hssf. usermodel. hssfworkbook workbook = new npoi. hssf. usermodel. hssfworkbook ();
Npoi. Ss. usermodel. isheet sheet = Workbook. createsheet ();
Npoi. Ss. usermodel. irow headerrow = sheet. createrow (0 );

// Set the 7-column width to 100
Sheet. setcolumnwidth (7,100 );
// Add the title

Headerrow. createcell (0). setcellvalue ("ad No ");
Headerrow. createcell (1). setcellvalue ("ad title ");
Headerrow. createcell (2). setcellvalue ("ad content ");
Headerrow. createcell (3). setcellvalue ("Advertiser ");
Headerrow. createcell (4). setcellvalue ("advertising phone ");
Headerrow. createcell (5). setcellvalue ("advertisement URL ");
Headerrow. createcell (6). setcellvalue ("advertisement pictures ");
Headerrow. createcell (7). setcellvalue ("image display ");
Headerrow. createcell (8). setcellvalue ("ad large image ");
Headerrow. createcell (9). setcellvalue ("image display ");
Headerrow. createcell (10). setcellvalue ("audit status ");

Int rowindex = 1;
Foreach (datarow row in sourcetable. Rows)
{
Npoi. Ss. usermodel. irow datarow = sheet. createrow (rowindex );
Datarow. Height = 50;

// The column height is 50.

Datarow. createcell (0). setcellvalue (row ["gg_id"]. tostring ());
Datarow. createcell (1). setcellvalue (row ["gg_title"]. tostring ());
Datarow. createcell (2). setcellvalue (row ["gg_memo"]. tostring ());
Datarow. createcell (3). setcellvalue (row ["ggs_name"]. tostring ());
Datarow. createcell (4). setcellvalue (row ["gg_tel"]. tostring ());
Datarow. createcell (5). setcellvalue (row ["gg_add"]. tostring ());
Datarow. createcell (6). setcellvalue (row ["p_name"]. tostring ());
String picurl = row ["p_url"]. tostring (); // image storage path
Datarow. createcell (8). setcellvalue (row ["gg_check"]. tostring ());
Addpiechart (sheet, workbook, picurl, rowindex, 7 );
Rowindex ++;
}
Workbook. Write (MS );
Ms. Flush ();
Ms. Position = 0;
Sheet = NULL;
Headerrow = NULL;
Workbook = NULL;
Return MS;
}

///
/// Insert an image to sheet
///
///
///
Private void addpiechart (isheet sheet, hssfworkbook workbook, string fileurl, int row, int col)
{
Try
{
// Add picture data to this workbook.
String Path = server. mappath ("~ /Html /");
If (fileurl. Contains ("/"))
{
Path + = fileurl. substring (fileurl. indexof ('/'));
}
String filename = path;
Byte [] bytes = system. Io. file. readallbytes (filename );

If (! String. isnullorempty (filename ))
{
Int pictureidx = Workbook. addpicture (bytes, npoi. Ss. usermodel. picturetype. JPEG );
Hssfpatriarch patriarch = (hssfpatriarch) sheet. createdrawingpatriarch ();
Hssfclientanchor anchor = new hssfclientanchor (0, 0,100, 50, Col, row, Col + 1, row + 1 );
// ## Process the photo position. [the upper left corner of the image is column Col + 1 in row + 1, and the lower right corner is column Col + 1 in row + 1) row + 1 + 1 column in col + 1, with a width of 100 and a height of 50

Hssfpicture PICT = (hssfpicture) patriarch. createpicture (anchor, pictureidx );

// Pict. Resize (); this sentence must not be used. It is displayed in the original image size.
}
}
Catch (exception ex)
{
Throw ex;
}
}
# Endregion

Npoi export Excel files with images and controllable sizes

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.