SoFunction
Updated on 2025-04-05

Specific implementation of table multi-table header export excel file in Jsp


/*
* The default conversion implementation function is required. If other functions are required, it needs to be expanded by itself.
* Parameters:
*           tableID: Table object id attribute value in HTML
* For detailed usage, please refer to the TableToExcel object definition below.
 */
function saveAsExcel(tableID){
 var tb = new TableToExcel(tableID);
  ("Courier New");
  (10);
  (2);
  (7);
  (false);
  (true);
  ();
}

/*
*  Function: Convert Table objects in HTML to Excel common objects.
*  Author: Jeva
*  Time: 2006-08-09
*  Parameters: tableID  The ID attribute value of the Table object in HTML
* illustrate:
*
*           Merge cells in Excel, the client needs to have Excel installed
*         For detailed attribute and method reference instructions, please refer to: Excel's Microsoft Excel Visual Basic Reference
*  Demonstration:
 *       var tb = new TableToExcel('demoTable');
 *    ("Courier New");
*    (10);  //Recommended value 10
*    (6);  //Generally, no settings are required
*   (4);  //Generally, no settings are required
*   (2);  //Recommended value 2
*    (10);  //Recommended value 10
 *    (false);
 *    (true);
 *   
 *    ();
*   If cell adaptation is set, the cell width is invalid
*  Version: 1.0
 */
function TableToExcel(tableID) {
= -1; //Border type, -1 has no border. 1/2/3/4 can be taken
= 0; //Background color: white   Can be used as the color number in the palette 1/2/3/4....
= 1;  //Font color: black
= 10;  //Font size
= "Songyi"; //Font type
= -1; //Road height
= -1; //Column width
= true; // Whether to wrap automatically
= -4108; //Content alignment   The default is centered
= false;  //Is it adaptable to width
    = tableID;
}

= function (excelBorder) {
    = excelBorder ;
};

= function (excelColor) {
    = excelColor;
};

= function (excelColor) {
    = excelColor;
};

= function (excelFontSize) {
    = excelFontSize;
};

= function (excelFont) {
    = excelFont;
};

= function (excelRowHeight) {
    = excelRowHeight;
};

= function (excelColumnWidth) {
    = excelColumnWidth;
};

= function (lineWrap) {
    if (lineWrap == false || lineWrap == true) {
        = lineWrap;
    }
};

= function (textAlign) {
    = textAlign;
};

= function(autoFit){
 if(autoFit == true || autoFit == false)
  = autoFit ;
}

//The main function of the file conversion
= function () {
    var jXls, myWorkbook, myWorksheet, myHTMLTableCell, myExcelCell, myExcelCell2;
    var myCellColSpan, myCellRowSpan;

    try {
        jXls = new ActiveXObject('');
    }
    catch (e) {
alert("Cannot start Excel!\n\n" + +
"\n\nIf you are sure that Excel is already installed on your computer,"+
"Then please adjust the security level of IE.\n\nSpecific operation:\n\n"+
"Tools → Internet Options → Security → Custom Levels → ActiveX Controls and Plug-ins \n\n" +
"→ Enable: Initialize and script run on ActiveX controls that are not marked as secure");
        return false;
    }

    = true;
    myWorkbook = ();
    = false;
    (3).Delete();
    (2).Delete();
    = true;
    myWorksheet = ;

    var  readRow = 0,  readCol = 0;
    var totalRow = 0, totalCol = 0;
    var   tabNum = 0;

//Set row height and column width
    if( != -1)
     = ;
    else
     = 7;
    if( != -1)
     = ;

//Search for Table objects that need to be converted to get the corresponding number of rows and columns
    var obj = ("table");
    for (x = 0; x < ; x++) {
        if (obj[x].id == ) {
            tabNum = x;
            totalRow = obj[x].;
            for (i = 0; i < obj[x].rows[0].; i++) {
                myHTMLTableCell = obj[x].rows(0).cells(i);
                myCellColSpan = ;
                totalCol = totalCol + myCellColSpan;
            }
        }
    }

//Start component simulation table
    var excelTable = new Array();
    for (i = 0; i <= totalRow; i++) {
        excelTable[i] = new Array();
        for (t = 0; t <= totalCol; t++) {
            excelTable[i][t] = false;
        }
    }

//Start conversion table
    for (z = 0; z < obj[tabNum].; z++) {
        readRow = z + 1;
        readCol = 1;
        for (c = 0; c < obj[tabNum].rows(z).; c++) {
            myHTMLTableCell = obj[tabNum].rows(z).cells(c);
            myCellColSpan = ;
            myCellRowSpan = ;
            for (y = 1; y <= totalCol; y++) {
                if (excelTable[readRow][y] == false) {
                    readCol = y;
                    break;
                }
            }
            if (myCellColSpan * myCellRowSpan > 1) {
                myExcelCell = (readRow, readCol);
                myExcelCell2 = (readRow + myCellRowSpan - 1, readCol + myCellColSpan - 1);
                (myExcelCell, myExcelCell2).Merge();
                = ;
                = ;
                = ;
                = ;
                = ;
                = ;
                if( != -1){
                 (myExcelCell, myExcelCell2).Borders(1).Weight = ;
                 (myExcelCell, myExcelCell2).Borders(2).Weight = ;
                 (myExcelCell, myExcelCell2).Borders(3).Weight = ;
                 (myExcelCell, myExcelCell2).Borders(4).Weight = ;
                }

                = ;
                for (row = readRow; row <= myCellRowSpan + readRow - 1; row++) {
                    for (col = readCol; col <= myCellColSpan + readCol - 1; col++) {
                        excelTable[row][col] = true;
                    }
                }

                readCol = readCol + myCellColSpan;
            } else {
                myExcelCell = (readRow, readCol);
                = ;
                = ;
                = ;
                = ;
                = ;
                = ;
                = ;
                if( != -1){
                 (1).Weight = ;
                 (2).Weight = ;
                 (3).Weight = ;
                 (4).Weight = ;
                }              
                excelTable[readRow][readCol] = true;
                readCol = readCol + 1;
            }
        }
    }
    if( == true)
     ;

    = true;
    jXls = null;
    myWorkbook = null;
    myWorksheet = null;
};