Asp. NET Operation Excel Summary Article _ practical Skill

Source: Internet
Author: User
Tags border color rar blank page
Ad 19XX years ago, on the operation of Excel, such as surging water, continuous, real operation of Excel I also started from the second half of last year, some more complex annual reports and so on, do a little laborious, but still can draw out, about Excel report export, In view of the export time consuming problem I mainly use Ajax to do, capture several starting and ending state, to the client 3 states: Retrieving Data ... ---"Prepare to export data ... (only successfully removed from the database, have not read and write Excel files)--"is reading and writing files-" The success of the export data, of course, if the process of error, there are corresponding prompts, so think of writing this article, mainly because this year has a system of some Excel operation also let me do, By the way, combined with the experience of Excel before the summary, may not be what, for the vast majority of no technical content, a search on the internet a lot, but I think it is necessary to sum up, at least to give the garden novice some help, Ok,let ' s go ...





First, the application of the program operation Excel or in the statistical reporting aspect, you may consider reads the Excel template, also may consider does not need to read the template, actually reads does not read the template to be able to achieve the same effect, sees the actual situation and uses.


1. Read the template, the first template stored in a path, according to the template of the data extracted from the database back to Excel and then generate a new Excel store is another path for download, the template unchanged.


I am here in the Excel operation is mainly in VS2005, VS2003 also can, but not how to study 03 of the operation (the article finally I will put 05 of the sample download address The demo before the package forgot to put a data generation file, just put in, not add is also can run, and the template file data slightly filtered under a partial control to seeIn VS05, operations Excel refers directly to. NET-self-contained COM components, the item's Bin directory will appear automatically when you add it





Interop.Excel.dll this DLL (need to install office2003 Excel, the following instructions and examples are based on office2003, different versions of the call may vary)


The namespace of the page references the using Excel;


The following is a section of code that invokes the template


Copy Code code as follows:



#region export Excel tables using templates


Case "Reportbytemp":


{





DataView dv = cache["Reportbytemp"] as DataView;


Create a new process of excel.application


Excel.Application app = new Excel.Application ();


if (app = null)


{


Return


}


App. Visible = false;


App. UserControl = true;


Workbooks workbooks = App. workbooks;


_workbook workbook = workbooks. Add (Template_path + "\\EXCEL Test template. xls");//The parameter in the Add method here is the path to the template


Sheets Sheets = workbook. worksheets;


_worksheet worksheet = (_worksheet) sheets.get_item (1);//template has only one sheet table


if (worksheet = null)


{


Return


}





int rownum = 0;


for (int i = 0; i < DV. Count; i++)


{


RowNum = i + 1;


Worksheet. Cells[3 + I, 1] = rownum;


Worksheet. Cells[3 + I, 2] = Dv[i]. Row[0]. ToString ();


Worksheet. Cells[3 + I, 3] = Dv[i]. ROW[1]. ToString ();





Exceloperate.setbold (worksheet, worksheet. Cells[3 + I, 1], worksheet. Cells[3 + I, 1]); Blackbody


Exceloperate.sethaligncenter (worksheet, worksheet. Cells[3 + I, 1], worksheet. Cells[3 + I, 3]);/center


Worksheet.get_range (worksheet. Cells[3 + I, 1], worksheet. Cells[3 + I, 3]). Borders.color = System.Drawing.ColorTranslator.ToOle (System.Drawing.Color.Black);





}





Tick = DateTime.Now.Ticks.ToString ();


Save_path = Temp_path + "\" + tick + ". xls";


Workbook. SaveAs (Save_path, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);


Exceloperate.dispose (worksheet, workbook, app);//close the Excel process





}


Break


#endregion





The effect is as follows:








2. Do not read the template, the call will actually inherit a blank template, and then write data, the program to draw the head of the table, and finally achieve the same effect, the program is as follows:


Copy Code code as follows:



#region generate Excel tables without using templates


Case "Reportbynone":


{





DataView dv = cache["Reportbynone"] as DataView;


Create a new process of excel.application


Excel.Application app = new Excel.Application ();


if (app = null)


{


Return


}


App. Visible = false;


App. UserControl = true;


Workbooks workbooks = App. workbooks;


_workbook workbook = workbooks. Add (xlwbatemplate.xlwbatworksheet);//The parameter in the Add method here is equivalent to inheriting an empty template (see this for a while)


Sheets Sheets = workbook. worksheets;


_worksheet worksheet = (_worksheet) sheets.get_item (1);


if (worksheet = null)


{


Return


}





Worksheet.get_range (worksheet. Cells[1, 1], worksheet. Cells[1, 3]). Merge (Missing.Value); Horizontal Merge


Worksheet.get_range (worksheet. Cells[1, 1], worksheet. Cells[1, 1]). Value2 = "Export Excel test One";


Exceloperate.setbold (worksheet, worksheet. Cells[1, 1], worksheet. Cells[1, 1]); Blackbody


Exceloperate.sethaligncenter (worksheet, worksheet. Cells[1, 1], worksheet. Cells[1, 1]);/center


Exceloperate.setbgcolor (worksheet, worksheet. Cells[1, 1], worksheet. Cells[1, 1], System.Drawing.Color.Red);//Background color


Exceloperate.setfontsize (worksheet, worksheet. Cells[1, 1], worksheet. Cells[1, 1], 16);//font size


Exceloperate.setrowheight (worksheet, worksheet. Cells[1, 1], worksheet. Cells[1, 1], 32.25);//Row height


Worksheet.get_range (worksheet. Cells[1, 1], worksheet. Cells[1, 1]). Borders.color = System.Drawing.ColorTranslator.ToOle (System.Drawing.Color.Black);//Black continuous border





Worksheet. Cells[2, 1] = "serial number";


Worksheet. Cells[2, 2] = "Company";


Worksheet. Cells[2, 3] = "department";


Exceloperate.setbold (worksheet, worksheet. Cells[2, 1], worksheet. Cells[2, 3]); Blackbody


Worksheet.get_range (worksheet. Cells[2, 1], worksheet. Cells[2, 3]). Borders.color = System.Drawing.ColorTranslator.ToOle (System.Drawing.Color.Black);


Exceloperate.sethalignright (worksheet, worksheet. Cells[2, 1], worksheet. Cells[2, 3]);


Exceloperate.setbgcolor (worksheet, worksheet. Cells[2, 1], worksheet. Cells[2, 3], System.Drawing.Color.Silver);//Background color


int rownum = 0;


for (int i = 0; i < DV. Count; i++)


{


RowNum = i + 1;


Worksheet. Cells[3 + I, 1] = rownum;


Worksheet. Cells[3 + I, 2] = Dv[i]. Row[0]. ToString ();


Worksheet. Cells[3 + I, 3] = Dv[i]. ROW[1]. ToString ();





Exceloperate.setbold (worksheet, worksheet. Cells[3 + I, 1], worksheet. Cells[3 + I, 1]); Blackbody


Exceloperate.sethaligncenter (worksheet, worksheet. Cells[3 + I, 1], worksheet. Cells[3 + I, 3]);/center


Worksheet.get_range (worksheet. Cells[3 + I, 1], worksheet. Cells[3 + I, 3]). Borders.color = System.Drawing.ColorTranslator.ToOle (System.Drawing.Color.Black);//Set border color, otherwise print preview will be very unsightly





}


Exceloperate.setcolumnwidth (worksheet, "A", 10);


Exceloperate.setcolumnwidth (worksheet, "B", 20);


Exceloperate.setcolumnwidth (worksheet, "C", 20);


Worksheet. Name = "Export Excel test One";





Tick = DateTime.Now.Ticks.ToString ();


Save_path = Temp_path + "\" + tick + ". xls";


Workbook. SaveAs (Save_path, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);


Exceloperate.dispose (worksheet, workbook, app);//close the Excel process





}


Break





#endregion





The effect is as follows:








I've given you two of the simplest instructions, and here's a little bit more about generating processing for some slightly more complex reports.





Two. For the complex Excel report generation processing, is nothing more than vertical merging of the same data rows and nested vertical merge and some other operations, the following on a number of targeted statements under description.


1. To generate a relatively complex Excel table, when fetching data from the database, you should pay attention to the reasonable requirements of the order, sometimes can be ordered by following several fields, and these fields who first have to pay attention, because these will directly affect the report rendering effect, For example, your Excel table to the monthly statistics of domestic and foreign projects, when you show up for multiple items, the same person is sequential, so the sort may have to be the order by month, Project category, user ID, Project ID (this is a written view, based on the view), this sort of sequence of fields can not be changed, It's not good to change. Generate the desired form, as shown in the following figure:





This is also a dynamic painting, with a simple template, the template is a header, not much meaning, unless the table header is very complex and do not need to repaint in the list, consider the template is better, to the above that January international and other months are needed to redraw the head of the table. As for merging, if it is not a nested merge, we can directly control the data when it is recycled to the template, such as the following simple wording:


Copy Code code as follows:



for (i = 0; i < table. Rows.Count; i++)


{


Bidname = table. rows[index]["Bidname"]. ToString ();


if (table. rows[i]["Bidname"]. ToString () = = Bidname)


{


projnum++;


Worksheet. Cells[5 + I, 2] = table. rows[i]["Projno"];


Worksheet. Cells[5 + I, 3] = table. rows[i]["PROJNAME"];


Worksheet. Cells[5 + I, 4] = table. rows[i]["Stat_date"];


Worksheet. Cells[5 + I, 5] = table. rows[i]["Projtype"];


Worksheet. Cells[5 + I, 6] = table. rows[i]["CONTENT"];


Worksheet. Cells[5 + I, 7] = table. rows[i]["Opendt"];


Worksheet. Cells[5 + I, 8] = table. rows[i]["Openaddress"];


Worksheet. Cells[5 + I, 9] = table. rows[i]["Rev_date"];


Worksheet. Cells[5 + I, +] = table. rows[i]["Bid_unit"];


Worksheet. Cells[5 + i, one] = table. rows[i]["Agt_amount"];


Worksheet. Cells[5 + i, [a] = table. rows[i]["CURRENCY"] + ":" + table. rows[i]["Bidser_amount"];


Worksheet. Cells[5 + i,] = table. rows[i]["Senddate"];


Worksheet. Cells[5 + I, +] = table. rows[i]["CURRENCY"] + ":" + table. rows[i]["Bidprice"];


Worksheet. Cells[5 + i,] = table. rows[i]["Bookamount"];


Worksheet. Cells[5 + i,] = table. rows[i]["CURRENCY"] + ":" + table. rows[i]["Bail_amount"];


Worksheet. Cells[5 + I, +] = table. rows[i]["USERNAME"];


Worksheet. Cells[5 + I, +] = table. rows[i]["Second_user"];


Worksheet. Cells[5 + i, 19] = "";


Worksheet.get_range (worksheet. Cells[5 + I, 1], worksheet. Cells[5 + I, 19]). Borders.color = System.Drawing.ColorTranslator.ToOle (System.Drawing.Color.Black);


Continue


}





Worksheet.get_range (worksheet. Cells[5 + ROWID, 1], worksheet. Cells[5 + i-1, 1]). Merge (Missing.Value); Merger of the first column by tender unit


Worksheet.get_range (worksheet. Cells[5 + ROWID, 1], worksheet. Cells[5 + ROWID, 1]). Value2 = Bidname + "(" + projnum.tostring () + "item)"/merged cell contents





When merging cells, it's also important to note that the merged cells must be empty. Otherwise, when you perform the merge, you are prompted that the value of the merged cell will be lost, which is roughly what you mean, generally we combine the same contents of all cells, and we save that value before merging, Then empty and merge, worksheet in the code above. cell[5+rowid,1] The values of the cells in the series are empty, the data is not written, and then the value is merged to avoid a circular emptying.


2. Nested merging to do the above may control the more troublesome, and the idea may be very confusing, we can consider a loop to populate all the data, in the loop to merge the columns, such as the following table





First, iterate through the data, as follows:


Copy Code code as follows:

int index = 0, rownum = 0;
String projno = "";
for (i = 0; i < table. Rows.Count; i++)
{
Projno = table. rows[index]["Projno"]. ToString ();
if (table. rows[i]["Projno"]. ToString () = = Projno)
{
Wksheet. Cells[3 + I, 1] = rownum + 1;
Wksheet. Cells[3 + I, 2] = "'" + table. rows[i]["Projno"]; With single quotes to ensure that characters that start with 0 are exported as they are
Wksheet. Cells[3 + I, 3] = "'" + table. rows[i]["PROJNAME"];
Wksheet. Cells[3 + I, 4] = "'" + table. rows[i]["Pa_name"];
Wksheet. Cells[3 + I, 5] = "'" + table. rows[i]["Bider_name"];
Wksheet. Cells[3 + I, 6] = table. rows[i]["Bail_amount"];
Wksheet. Cells[3 + I, 7] = table. rows[i]["Not_back"];
Wksheet.get_range (Wksheet. Cells[3 + I, 1], Wksheet. Cells[3 + I, 7]). Borders.color = System.Drawing.ColorTranslator.ToOle (System.Drawing.Color.Black);
Continue
}
index = i;
rownum++;
i--;
}



The following merge cells with the same contents in the first three columns:


Copy Code code as follows:



Merging the first three columns of operations


int m = 1, rowID = 3, k;


String projName = "";


for (k = 3; k &lt;= i + 2; k++)


{


if (Convert.ToInt32 (Wksheet.get_range) (Wksheet. Cells[k, 1], Wksheet. Cells[k, 1]). Value2) = = m)


{


Projno = Wksheet.get_range (wksheet. Cells[k, 2], Wksheet. Cells[k, 2]). Value2.tostring ();


ProjName = Wksheet.get_range (wksheet. Cells[k, 3], Wksheet. Cells[k, 3]). Value2.tostring ();


Wksheet.get_range (Wksheet. Cells[k, 1], Wksheet. Cells[k, 1]). Value2 = "";


Wksheet.get_range (Wksheet. Cells[k, 2], Wksheet. Cells[k, 2]). Value2 = "";


Wksheet.get_range (Wksheet. Cells[k, 3], Wksheet. Cells[k, 3]). Value2 = "";


Continue


}


Wksheet.get_range (Wksheet. Cells[rowid, 1], Wksheet. Cells[k-1, 1]). Merge (Missing.Value);


Wksheet.get_range (Wksheet. Cells[rowid, 1], Wksheet. Cells[rowid, 1]). Value2 = m;


Wksheet.get_range (Wksheet. Cells[rowid, 2], Wksheet. Cells[k-1, 2]). Merge (Missing.Value);


Wksheet.get_range (Wksheet. Cells[rowid, 2], Wksheet. Cells[rowid, 2]). Value2 = "'" + projno;


Wksheet.get_range (Wksheet. Cells[rowid, 3], Wksheet. Cells[k-1, 3]). Merge (Missing.Value);


Wksheet.get_range (Wksheet. Cells[rowid, 3], Wksheet. Cells[rowid, 3]). Value2 = "'" + projName;


m++;


rowID = k;


k--;


}


Merge the last bidding project after jumping out of circulation


Wksheet.get_range (Wksheet. Cells[rowid, 1], Wksheet. Cells[k-1, 1]). Merge (Missing.Value);


Wksheet.get_range (Wksheet. Cells[rowid, 1], Wksheet. Cells[rowid, 1]). Value2 = m;


Wksheet.get_range (Wksheet. Cells[rowid, 2], Wksheet. Cells[k-1, 2]). Merge (Missing.Value);


Wksheet.get_range (Wksheet. Cells[rowid, 2], Wksheet. Cells[rowid, 2]). Value2 = "'" + projno;


Wksheet.get_range (Wksheet. Cells[rowid, 3], Wksheet. Cells[k-1, 3]). Merge (Missing.Value);


Wksheet.get_range (Wksheet. Cells[rowid, 3], Wksheet. Cells[rowid, 3]). Value2 = "'" + projName;





Merge the header columns below


Copy Code code as follows:



Merge the header columns


index = 0; ROWID = 3; Resetting variables


String pa_name = String. Empty; Label Segment Name


for (k = 3; k &lt;= i + 2; k++)


{


Pa_name = table. rows[index]["Pa_name"]. ToString ();


if (Wksheet.get_range (Wksheet). Cells[k, 4], Wksheet. Cells[k, 4]). value2.tostring () = = Pa_name)


{


Wksheet.get_range (Wksheet. Cells[k, 4], Wksheet. Cells[k, 4]). Value2 = "";


Continue


}


Wksheet.get_range (Wksheet. Cells[rowid, 4], Wksheet. Cells[k-1, 4]). Merge (Missing.Value);


Wksheet.get_range (Wksheet. Cells[rowid, 4], Wksheet. Cells[rowid, 4]). Value2 = "'" + pa_name;


index = k-3;


rowID = k;


k--;


}


Merge the last item's section when exiting a loop


Wksheet.get_range (Wksheet. Cells[rowid, 4], Wksheet. Cells[k-1, 4]). Merge (Missing.Value);


Wksheet.get_range (Wksheet. Cells[rowid, 4], Wksheet. Cells[rowid, 4]). Value2 = "'" + pa_name;


Tick = DateTime.Now.ToString ("Yyyymmddhhmmss");


Save_path = Temp_path + "\" + tick + "margin withdrawal table. xls";


session["Bailbackid"] = tick + "margin receipt and refund situation table. xls";


session["_bailback"] = "true";


Workbook. SaveAs (Save_path, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);


Exceloperate.dispose (worksheet, workbook, app);//close the Excel process


DownLoad (Save_path);


Page_close ();





Of course, the above operation will be a few cycles, in the performance is not very desirable, the garden brother may have a better way, the younger brother is not hesitate to enlighten


Let's look at some of the following effects:

















(Note: The exported data here refers to the successful extraction of data from the database, has not yet manipulated the Excel object, the beginning has been said, of course, this hint text replaced with other can also)














The whole process is AJAX-prompted, not refreshed, secondly, the export time is longer, you can give customers a good experience, no, the user points out the button, a half-day did not respond also did not prompt, customers feel how so slow, is not your program has a problem, accusing a lot of, have so much interactive message, Let the customer wait a few minutes also can withstand.





3. The resulting table contains multiple sheet operations, such as the following








The requirement to draw this table is based on the choice of months to months of the year, generate a comprehensive sheet for this few months, and then generate separate sheet tables for these months, generating a template for the table above, containing two sheet, a sheet for a combined month, and a sheet for a single month. Because the sheet representations of the individual months are the same, we can copy a few sheet according to the number of months selected.


Copy Code code as follows:

Workbooks workbooks = App. workbooks;
_workbook workbook = workbooks. ADD (Template_path + "\ * Tender for the annual tender situation monthly tables. xls");
Sheets Sheets = workbook. worksheets;
_worksheet Yearsheet = (_worksheet) sheets.get_item (1);
_worksheet worksheet = (_worksheet) Sheets.get_item (2);
if (worksheet = null)
{
Return
}
for (int i = 1; i < Monthcount; i++)
Worksheet. Copy (Missing.Value, workbook. WORKSHEETS[2]);/monthly statistics work thin



Yearsheet operation is not said, and the previous several operations, the key is the month of sheet generation, in fact, is the cycle of operation Get_item (i), the code is as follows


Copy Code code as follows:



Monthly detailed Statistics////////////////////////////////////


int item_id = 2;


rownum = 0; Book_amount = 0; index = 0;


Bid_amount = ""; Bidser_amount = ""; Agent_amount = 0;//Empty variable


_worksheet ws = NULL;


for (int i = 0; i &lt; TableMM.Rows.Count; i++)


{


rownum++;


Month = tablemm.rows[index]["Date_month"]. ToString ();


if (tablemm.rows[i]["Date_month"]. ToString () = = Month)


{


WS = (_worksheet) sheets.get_item (item_id);


Ws. Cells[3 + rowNum-1, 1] = rownum;


Ws. Cells[3 + rowNum-1, 2] = tablemm.rows[i]["Projno"];


Ws. Cells[3 + rowNum-1, 3] = tablemm.rows[i]["PROJNAME"];


Ws. Cells[3 + rowNum-1, 4] = tablemm.rows[i]["Bid_type"];


Ws. Cells[3 + rowNum-1, 5] = tablemm.rows[i]["Bid_mode"];


Ws. Cells[3 + rowNum-1, 6] = tablemm.rows[i]["Opendt"];


Ws. Cells[3 + rowNum-1, 7] = tablemm.rows[i]["openaddress"];


Ws. Cells[3 + rowNum-1, 8] = tablemm.rows[i]["Bid_unit"];


Ws. Cells[3 + rowNum-1, 9] = tablemm.rows[i]["Notice_no"]. ToString (). Replace ("Shenhua International Trade", "");


Ws. Cells[3 + rowNum-1, +] = tablemm.rows[i]["Bookamount"];


Ws. Cells[3 + rowNum-1, one] = tablemm.rows[i]["Bidprice"] + "(million" + tablemm.rows[i]["CURRENCY"] + ")";


Ws. Cells[3 + rowNum-1, a] = tablemm.rows[i]["Bidser_amount"] + "(million" + tablemm.rows[i]["CURRENCY"]+ ")";


Ws. Cells[3 + rowNum-1] = tablemm.rows[i]["Agt_amount"];


Ws. Cells[3 + rowNum-1, 14] = "";


Ws.get_range (ws. Cells[3 + rowNum-1, 1], WS. Cells[3 + rowNum-1, 14]). Borders.color = System.Drawing.ColorTranslator.ToOle (System.Drawing.Color.Black);


Continue


}


Ws. Cells[1, 1] = year + "years" + Bidname + getmonth (Month) + "Month of tender items list";


Total per month


sql = "Select CURRENCY, NVL (sum (bidprice), 0) as BIDPRICE,NVL (sum (bookamount), 0) as BOOKAMOUNT,NVL (sum (bidser_amount), 0 ) as BIDSER_AMOUNT,NVL (SUM (Agt_amount), 0) as Agt_amount from Ibs_v_bid_monthly_stat "+ Sqlfilter +


"and Date_year = '" + year + "' and company_id=" + Biderid + "and Date_month = '" + MONTH + "'" +


"GROUP by CURRENCY";


System.Data.DataTable dt1 = oraclehelper.retdatatable (sql);


for (int m = 0; m &lt; dt1. Rows.Count; m++)


{


Bid_amount + = Dt1. rows[m]["Bidprice"] + "(Million" +dt1. rows[m]["CURRENCY"] + ") \r\t";


Book_amount + = float. Parse (DT1. rows[m]["Bookamount"]. ToString ());


Bidser_amount + = Dt1. rows[m]["Bidser_amount"] + "(million" + DT1. rows[m]["CURRENCY"] + ") \r\t";


Agent_amount + = float. Parse (DT1. rows[m]["Agt_amount"]. ToString ());


}


Ws. Cells[3 + rowNum-1, 3] = "Total";


Ws. Cells[3 + rowNum-1, +] = Book_amount;


Ws. Cells[3 + rowNum-1, one] = Bid_amount;


Ws. Cells[3 + rowNum-1 = bidser_amount;


Ws. Cells[3 + rowNum-1 = agent_amount;


Ws.get_range (ws. Cells[3 + rowNum-1, 1], WS. Cells[3 + rowNum-1, 14]). Borders.color = System.Drawing.ColorTranslator.ToOle (System.Drawing.Color.Black);


Ws. Name = GETMM (Month);


item_id++;


index = i; Summary of tender items for the next month


i--;


rownum = 0; Book_amount = 0;


Bid_amount = ""; Bidser_amount = ""; Agent_amount = 0;//Empty variable


}


Summary of items for the last month when you jump out of a loop





With an Oracle database, so the above SQL statement ... Oh


============================================================================================


Above roughly said almost, because it is the cycle of what, perhaps for large data reading and writing, better performance, if you have any better method, you can guide, in order to make up the waiting time is too long, so the combination of Ajax to deal with.


Finally I put a small demo to do the link posted out to everyone, there are also a number of empty templates and corresponding generated data table for everyone to look after, especially relatively complex some of the table painting should be able to draw out, mainly to see what kind of methods we use, can be less cycle once as little circulation, hehe ~ ~ ~


Excel template Read and write instructions


Http://www.justlike.com.cn/upfiles/template_xls.rar


Http://www.justlike.com.cn/upfiles/ExcelFiles.rar


Http://www.justlike.com.cn/upfiles/ExcelReportDemo.rar


(Note: The last pop-up download file of a page always want to let it automatically turn off, but no, if you do not turn off, and then point to export, will not eject the download box, the actual processing we can put a download button next to the export, as the above effect picture, of course, you can point to export the time to open the page directly, Click to download the time to eject the download box, but directly open, the file needs to be generated in the virtual directory, not too safe, hehe ~ ~, see the actual situation dealt with)


==========================================================================================


Today's supplementary note, with regard to the effect of invoking Ajax callbacks, there is a place where the so-called "Ajax nesting call" is used, as follows





Copy Code code as follows:

function Excelreportcallback (RESP)
{
if (Resp.value = = "OK")
{
$ (' tipmsg '). InnerHTML = "<font color= #FF0000 style= Font-weight:bold> ready to export data, please wait for </font> ";
SetTimeout ("RedirectURL ()", 1000);//delay experience
}
Else
if (Resp.value = "NO")
{
$ (' tipmsg '). InnerHTML = "<font color= #FF0000 style=font-weight:bold> did not find the data </font>" in accordance with the query criteria;
$ (' btnexcel '). Disabled = false;
}
Else
{
$ (' tipmsg '). InnerHTML = "<font color= #FF0000 style=font-weight:bold> Warning: Error exporting data </font>";
$ (' btnexcel '). Disabled = false;
}

}



Copy Code code as follows:



function RedirectURL ()


{


$ (' tipmsg '). InnerHTML = "&lt;img border=\" 0\ "src=\" images/ajaxloading.gif\ "&gt;&lt;font color= #7fffd4 style= Font-weight:bold&gt; is reading and writing the report file, please &lt;/font&gt; later;


var ajax = new Ajax_request ("excelreport.aspx?flag=reportbytemp&amp;" +math.random (), "", "", reportcallback);


function Reportcallback (RESP)


{


if (resp.value!= "Error" &amp;&amp; resp.value!= "")


{


$ (' btnexcel '). Disabled = false;


$ (' tipmsg '). InnerHTML = "&lt;font color= #FF0000 style=font-weight:bold&gt; Data export Success!&lt;/font&gt;";


Open ("xls_download.aspx?path=" +resp.value);//window.location.href = resp.value;//


}


Else


{


$ (' btnexcel '). Disabled = false;


$ (' tipmsg '). InnerHTML = "&lt;font color= #FF0000 style=font-weight:bold&gt; File read-write error, check whether the file template exists or has read and write permission to the file!&lt;/font &gt; ";


}


}


}


function Open (URL)


{


window.open (URL, ' NewWindow ', ' height=1,width=1,top=1500,left=1500,toolbar=no,menubar=no,scrollbars=yes,location =no,status=no ')


}





Excelreportcallback (RESP) was originally a callback function, but it called a RedirectURL () method, which included a callback function, which formed the nesting of callbacks, because The first callback is to handle the success of fetching data from the database, if you successfully jump to the page to draw Excel, such words will appear a blank page, etc. after the production of the download box, and then feel that you can nest a callback to continue an asynchronous operation, so there will be no long waiting for the blank page, But to generate a good Excel return address, or can return a filename to xls_download.aspx page directly download, but xls_download.aspx is also to appear, I tried to let the download after this page automatically closed, rogue can not, so put open () The data value in the method makes the page not appear, but the status bar is still displayed.


Here is finished, decided to luxury, put to the home page:), always feel that the first page of the article only master can put, but also put to the home page is a luxury, hope that some people in the garden have some help it ~ ~


Http://xiazai.jb51.net/201102/yuanma/ExcelReport.rar
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.