Asp.net:

Source: Internet
Author: User

Asp tutorial. net how to export images in excel and cells:

In the asp.net tutorial, there are two ways to export excel. One is to store the exported file under a folder on the server, and then output the file address to the browser; one is to directly write the file output stream to the browser. In response output, t-separated data. In excel export, n is equivalent to line feed.
1. Output all html files to excel

This method outputs all html content, such as buttons, tables, and images, to an excel file.

Response. clear ();
Response. buffer = true;
Response. appendheader ("content-disposition", "attachment; filename =" + datetime. now. tostring ("yyyymmdd") + ". xls ");
Response. contentencoding = system. text. encoding. utf8;
Response. contenttype = "application/vnd. ms-excel ";
This. enableviewstate = false;


Here, we use the contenttype attribute, which defaults to text/html. At this time, the output is hypertext, that is, our common webpage format is sent to the client, if it is changed to ms-excel, the excel format will be output, that is, the workbook format will be output to the client, then the browser will prompt you to download and save. Contenttype attributes include: image/jpeg; text/html; image/gif; vnd. ms-excel/msword. Likewise, we can also output (export) images and Word documents. The following method also uses this attribute.

 

2. Export data in the datagrid Control to excel

Although the above method achieves the export function, it also imports all the output information in html such as buttons and paging boxes. We generally want to export data on the datagrid Control.

System. web. ui. control ctl = this. datagrid1;
// Datagrid1 is the control you drag and drop in the form
Httpcontext. current. response. appendheader ("content-disposition", "attachment?filename=excel.xls ");
Httpcontext. current. response. charset = "UTF-8 ";
Httpcontext. current. response. contentencoding = system. text. encoding. default;
Httpcontext. current. response. contenttype = "application/ms-excel ";
Ctl. page. enableviewstate = false;
System. io. stringwriter tw = new system. io. stringwriter ();
System.web.ui.html textwriter hw = new system.web.ui.html textwriter (tw );
Ctl. rendercontrol (hw );
Httpcontext. current. response. write (tw. tostring ());
Httpcontext. current. response. end ();

If your datagrid uses paging, it exports the information of the current page, that is, it exports the information displayed in the datagrid. Instead of all the information of your select statement.

For ease of use, the writing method is as follows:

Public void dgtoexcel (system. web. ui. control ctl)
{
Httpcontext. current. response. appendheader ("content-disposition", "attachment?filename=excel.xls ");
Httpcontext. current. response. charset = "UTF-8 ";
Httpcontext. current. response. contentencoding = system. text. encoding. default;
Httpcontext. current. response. contenttype = "application/ms-excel ";
Ctl. page. enableviewstate = false;
System. io. stringwriter tw = new system. io. stringwriter ();
System.web.ui.html textwriter hw = new system.web.ui.html textwriter (tw );
Ctl. rendercontrol (hw );
Httpcontext. current. response. write (tw. tostring ());
Httpcontext. current. response. end ();
}
Usage: dgtoexcel (datagrid1 );


3. export data from dataset to excel

With the above idea, we can export the exported information to the response client. Export the data in the dataset, that is, to export the information of each row in the table in the dataset in the ms-excel format to the http stream, so that it is OK. Dsds datasdatasdatasdatasdatasdatasdatasdatasdatas( datasds dsdatasdatasdatasdatasdatasdatasdatasdatasdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsds

Public void createexcel (dataset ds, string filename)
{
Httpresponse resp;
Resp = page. response;
Resp. contentencoding = system. text. encoding. getencoding ("gb2312 ");
Resp. appendheader ("content-disposition", "attachment; filename =" + filename );
String colheaders = "", ls_item = "";
 
// Define the table object and row object, and use dataset to initialize the value.
Datatable dt = ds. tables [0];
Datarow [] myrow = dt. select (); // you can filter data in the form of dt. select ("id> 10 ").
Int I = 0;
Int cl = dt. columns. count;

// Obtain the titles of each column in the data table. The headers are separated by t. A carriage return is followed by the last column title.
For (I = 0; I {
If (I = (cl-1) // The last column, plus n
{
Colheaders + = dt. columns [I]. caption. tostring () + "n ";
}
Else
{
Colheaders + = dt. columns [I]. caption. tostring () + "t ";
}

}
Resp. write (colheaders );
// Write the obtained data to the http output stream

// Process data row by row
Foreach (datarow row in myrow)
{
// Write the data in the current row to the http output stream, and leave ls_item empty for downstream data
For (I = 0; I {
If (I = (cl-1) // The last column, plus n
{
Ls_item + = row [I]. tostring () + "n ";
}
Else
{
Ls_item + = row [I]. tostring () + "t ";
}
}
Resp. write (ls_item );
Ls_item = "";
}
Resp. end ();
}

I. Method for exporting execl in asp.net:

There are two ways to export excel in asp.net: one is to store the exported file under a folder on the server, and then output the file address to the browser; one is to directly write the file output stream to the browser. In response output, t-separated data. In excel export, n is equivalent to line feed.
1. Output all html files to excel

This method outputs all html content, such as buttons, tables, and images, to an excel file.
Response. clear ();
Response. buffer = true;
Response. appendheader ("content-disposition", "attachment; filename =" + datetime. now. tostring ("yyyymmdd") + ". xls ");
Response. contentencoding = system. text. encoding. utf8;
Response. contenttype = "application/vnd. ms-excel ";
This. enableviewstate = false;


Here, we use the contenttype attribute, which defaults to text/html. At this time, the output is hypertext, that is, our common webpage format is sent to the client, if it is changed to ms-excel, the excel format will be output, that is, the workbook format will be output to the client, then the browser will prompt you to download and save. Contenttype attributes include: image/jpeg; text/html; image/gif; vnd. ms-excel/msword. Likewise, we can also output (export) images and Word documents. The following method also uses this attribute.

 

2. Export data in the datagrid Control to excel

Although the above method achieves the export function, it also imports all the output information in html such as buttons and paging boxes. We generally want to export data on the datagrid Control.
System. web. ui. control ctl = this. datagrid1;
// Datagrid1 is the control you drag and drop in the form
Httpcontext. current. response. appendheader ("content-disposition", "attachment?filename=excel.xls ");
Httpcontext. current. response. charset = "UTF-8 ";
Httpcontext. current. response. contentencoding = system. text. encoding. default;
Httpcontext. current. response. contenttype = "application/ms-excel ";
Ctl. page. enableviewstate = false;
System. io. stringwriter tw = new system. io. stringwriter ();
System.web.ui.html textwriter hw = new system.web.ui.html textwriter (tw );
Ctl. rendercontrol (hw );
Httpcontext. current. response. write (tw. tostring ());
Httpcontext. current. response. end ();

If your datagrid uses paging, it exports the information of the current page, that is, it exports the information displayed in the datagrid. Instead of all the information of your select statement.

For ease of use, the writing method is as follows:
Public void dgtoexcel (system. web. ui. control ctl)
{
Httpcontext. current. response. appendheader ("content-disposition", "attachment?filename=excel.xls ");
Httpcontext. current. response. charset = "UTF-8 ";
Httpcontext. current. response. contentencoding = system. text. encoding. default;
Httpcontext. current. response. contenttype = "application/ms-excel ";
Ctl. page. enableviewstate = false;
System. io. stringwriter tw = new system. io. stringwriter ();
System.web.ui.html textwriter hw = new system.web.ui.html textwriter (tw );
Ctl. rendercontrol (hw );
Httpcontext. current. response. write (tw. tostring ());
Httpcontext. current. response. end ();
}
Usage: dgtoexcel (datagrid1 );

3. export data from dataset to excel

With the above idea, we can export the exported information to the response client. Export the data in the dataset, that is, to export the information of each row in the table in the dataset in the ms-excel format to the http stream, so that it is OK. Dsds datasdatasdatasdatasdatasdatasdatasdatasdatas( datasds dsdatasdatasdatasdatasdatasdatasdatasdatasdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsds

Public void createexcel (dataset ds, string filename)
{
Httpresponse resp;
Resp = page. response;
Resp. contentencoding = system. text. encoding. getencoding ("gb2312 ");
Resp. appendheader ("content-disposition", "attachment; filename =" + filename );
String colheaders = "", ls_item = "";
 
// Define the table object and row object, and use dataset to initialize the value.
Datatable dt = ds. tables [0];
Datarow [] myrow = dt. select (); // you can filter data in the form of dt. select ("id> 10 ").
Int I = 0;
Int cl = dt. columns. count;

// Obtain the titles of each column in the data table. The headers are separated by t. A carriage return is followed by the last column title.
For (I = 0; I {
If (I = (cl-1) // The last column, plus n
{
Colheaders + = dt. columns [I]. caption. tostring () + "n ";
}
Else
{
Colheaders + = dt. columns [I]. caption. tostring () + "t ";
}

}
Resp. write (colheaders );
// Write the obtained data to the http output stream

// Process data row by row
Foreach (datarow row in myrow)
{
// Write the data in the current row to the http output stream, and leave ls_item empty for downstream data
For (I = 0; I {
If (I = (cl-1) // The last column, plus n
{
Ls_item + = row [I]. tostring () + "n ";
}
Else
{
Ls_item + = row [I]. tostring () + "t ";
}
}
Resp. write (ls_item );
Ls_item = "";
}
Resp. end ();
}

For many objects in excel, such as cell, shape, and chart, you sometimes need to save them as an image. Just like.
Recently, I was working on an excel-related project. When a project encountered a very abnormal requirement, I needed to take a picture of some objects in the excel file. For example, after setting some colors for a cell, I took a picture, you can also create a report as an image for a chart. After twists and turns, I finally finished it. Share it with you.
To use excel, you must first view the com Object Model of excel. Here:
Http://msdn.microsoft.com/en-us/library/bb149081 (v = office.12). aspx
Here we will explain:
The official excel2010 object reference is not found. If you click it, it is an endless loop. If you click it, you cannot find it. If you find someone else, please reply.
The object model of excel2003 needs to be installed. It is troublesome. I only know it after installation. Here we recommend that you view the excel2007 model through the above URL. In fact, according to Microsoft's compatibility practices, excel2010 and excel2003 should be slightly different (right ?).
Then it's nice to check the members of the range object and expect it to have any ready-made method: http://msdn.microsoft.com/en-us/library/bb225606 (v = office.12). aspx
I checked a circle and didn't find any methods like exportimage and saveimage. It means frustration, but it is also reasonable.
A bright spot is found in despair. The name of a method is copypicture. After reading the instructions, we copied the object to the clipboard as an image. A distorted idea was born. Since it can be copied to the clipboard, can I just copy the picture from the clipboard.
Okay, that's the deal. Just do what you say.
......
200 words are omitted here (if you do not know how to create an excel com Object and retrieve the range object, you can also reply to the question if you do not know how to query it yourself .)
......
After obtaining the range object. Two parameters are required to call the copypicture method. The first parameter is the xlpictureappearance enumeration. 1 indicates copying according to the screen, and 2 indicates copying according to the print. The second parameter is xlcopypictureformat enumeration. 2 indicates copying to a bitmap, and-4147 indicates copying to a vector image.
As a result, I wrote code similar to the following.

Range cell = excel. activesheet. cells [1, 1];
Cell. copypicture (1, 2 );
Bitmap image = clipboard. getimage () as bitmap;
If (image! = Null)
{
// You can do whatever you want here.
}

Due to some restrictions of. net, the data copied to the clipboard by some old programs (such as excel2007) may be unavailable and needs to be used through local APIs. I'm going... I haven't seen a half-word reminder on msdn.
The landlord is a very diligent person. If he knows this, he will not be able to do it quickly.

Intptr hwnd = excel. hwnd;
Try & nbsp;

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.