Perfect solution for controlling Excel printing with JavaScript

Source: Internet
Author: User

First, if the client calls excel, it must reduce the ie security level, so some illegal controls, such as 3721, are automatically installed, So I solve this problem: keep the original ie settings, add your site to a trusted site, and then change the security level of the secure site. In this way, a secure channel is set up for your site and illegal intrusion of 3721 is prevented, of course, the premise is that you want your customers to download a registry and install it. I do it myself and use it myself, so I have to follow me when I say everything, you need to convince the customer :).

Well, the next step is how to print it. Here I have won the report very much, and some very complicated ones, such as the student transcript. I need to calculate the credit points and print photos based on the academic year and semester, it is very troublesome for me to print the complete information to the client in terms of degree granting, student status handling, Grade 4 and grade 6, internship, minor score, and practice, I used excel to create a template first, and then called the template to fill in the data. The problem arises again. How can I organize so much data? For a long time, I suddenly had an idea, write an object separately for data extraction, so that my program is much simpler, and it is best for me to use it anywhere. I also said that the transcript shows that the data is very big and 150 pieces of data are required. Therefore, I chose a table as the data source, so that I do not have to generate any xml, and the data I want is displayed. OK to introduce Gettabledata. js: it is the data object I want. The main function is to extract data from the table GetTableData), and provide a method for outputting data getfielddata ), this basic function can meet most of my needs. This is an introduction I wrote by myself, for fear I forgot it later :). Let's take a look at what I don't know.

// The Obj parameter specifies the data source (Table defined). The first row of the field name is used by default.
// MoveNext method. The parameter is the number of digits that the table row moves up or down. A positive number moves down and a negative number moves up. For example, MoveNext (1) and. MoveNext (-1)
// The GetFieldData method is used to obtain the number of specified column names in the current row, for example,. GetFieldData ("exam result"): column name. GetFieldData (1): Index
// The Sort_desc method sorts the specified columns in descending order, for example,. Sort_desc ("School Year", "Semester ")
// The Sort_asc method is used to sort the specified columns in ascending order, for example,. Sort_asc ("score: number"): ":" The following parameter is used to change the column data type. This example is a number.
// The GetData method returns an array of data with a specific field value. If no field value exists, all records of this field are returned. you can perform other operations externally. We do not recommend that you process data directly, for example. getData ()
// Delete the current record, and Delete a row from the array, for example,. Delete
// UpdateField updates the specified index data index of the current row. If it is a number, it is the column index, which is a character and the name of the data field) for example: MyData. updateField ("course No.", MyData. getFieldData ("original course number "))
// Move the MoveFirst record to the first record of the data. Note that the data starts from the second row and the first row of the field name is used by default.
// Move the MoveLast record to the last record of the data
// Distinct returns the array of condition number dimensions for different records of the selected record and sorts the records in ascending order.
// Initialization, Obj: table Name, Leftlen: length of excess data on the left, Rightlen: length of excess data on the right,
// Rows attribute: number of rows in the array, cols: Number of Columns

In the sample code, Print_bzcjd.js prints a complex excel script,

 
 
  1. var xlBook = xls.Workbooks.Open("c:\\xsxycjb.xls");  
  2. var xlsheet = xlBook.Worksheets(1) 

This is a network template,

 
 
  1. var xlBook = xls.Workbooks.Open("http://you.com/\PrintTemplates\\xsxycjb.xls");  
  2. var xlsheet = xlBook.Worksheets(1) 

In this example, we basically show all the technologies required for excel printing, such as film and exit.

It should be noted that the table data can be hidden or retrieved without being displayed. In this way, a large amount of data can be added without affecting the interface.

Excel. js: This is the excel cleanup script. When the excel process is finished, it has not been released yet. If you clean it up, there will be no :.

This is a simple example. You can give a table name and then generate a table like this: MakeExcel ("TABLE1", true, "1.xls "), other parameters are optional.

 
 
  1. FunctionMakeExcel (Obj, ProceVisible, SaveAsFilename, SortType, SortField ){
  2. VarMyData =NewGetTableData (Obj, 0, 0 );
  3. VarMyrows = MyData. rows;
  4. VarI, j;
  5.  
  6. Try{
  7. VarXls =NewActiveXObject ("Excel. Application");
  8. }
  9. Catch(E ){
  10. Alert ("To print this table, you must install the Excel spreadsheet software and use the" ActiveX control "in the browser. Your browser must allow the execution of the control. Click Help to learn about browser settings! ");
  11. Return "";
  12. }
  13. If(Typeof(ProceVisible) ="Undefined"){
  14. Xls. visible =True;
  15. }
  16. Else{
  17. If(ProceVisible ="True"){
  18. Xls. visible =True;
  19. }
  20. Else 
  21. {
  22. Xls. visible =False;
  23. }
  24. }
  25. VarXlBook = xls. Workbooks. Add;
  26. VarXlsheet = xlBook. Worksheets (1 );
  27. Xls. Cells. Select;
  28. Xls. Selection. NumberFormatLocal ="@";
  29. For(I = 0; IFor(J = 0; jIf(Typeof(MyData. GetFieldData (j) ="Number"){
  30. Xlsheet. Cells (I + 1, j + 1). NumberFormatLocal =0.00 _";
  31. }
  32. Xlsheet. Cells (I + 1, j + 1). Value = MyData. GetFieldData (j );
  33. }
  34. MyData. MoveNext ();
  35. }
  36. If(SaveAsFilename =""|Typeof(SaveAsFilename) ="Undefined"){
  37. VarFname = xls. Application. GetSaveAsFilename ("*. Xls","Excel Spreadsheets (*. xls), *. xls");
  38. If(Fname! =""){
  39. XlBook. SaveAs (fname );
  40. }
  41. }
  42. Else{
  43. XlBook. SaveAs (SaveAsFilename );
  44. }
  45. XlBook. Close (savechanges =False);
  46. // Xls. visible = false; 
  47. Xls. Quit ();
  48. Xls =Null;
  49. // Ends the excel process and exits 
  50. Window. setInterval ("Cleanup ();", 1 );
  51. }

Edit recommendations]

  1. Document and window objects in JavaScript
  2. Introduction to the simulation of object-oriented technology in JavaScript
  3. Memory leakage of IE and the release of JavaScript Memory
  4. How to optimize the performance of JavaScript scripts
  5. The most common 25 classic tips in Javascript

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.