js導出頁面到excel和word

js導出頁面到excel和word(適用性不大,保存備用)

(導出需要ie瀏覽器並進行設置,當縱向合併和橫向合併並存時會出現問題,最好不要有縱向合併)

////////////////////////////////////////////////導出到excel

function OnExport()
        {
            var loadMarsk = new Ext.LoadMask(document.body,
            {
                   msg : '正在讀入導出數據。。。('+Date()+')',
                   removeMask :true// 完成後移除
            });
            loadMarsk.show();
            try {
                excelApp = new ActiveXObject("Excel.Application");
            } 
            catch (e) {
                alert("無法啓動Word!\n\n" + e.message +
                "\n\n如果您確信您的電腦中已經安裝了Excel," +
                "那麼請將本站點添加至IE的可信任站點,並調整可信任站點的安全級別。");
                return false;
            }
            excelApp.Application.Visible = true;
            var workbook = excelApp.Workbooks.Add();
            var sheet = workbook.Sheets(1);
            sheet.Application.ActiveSheet.PageSetup.LeftMargin= 1.5;         
        sheet.Application.ActiveSheet.PageSetup.RightMargin = 1.5;      
            var tableDom = $("form_table");
            var nRowCount = tableDom.rows.length;
            for (var nRow = 0; nRow < nRowCount - 1; ++nRow) {
                var nColCount = tableDom.rows[nRow].cells.length;
                var nExtCol = 0;
                for (var nCol = 0; nCol < nColCount; ++nCol) {
                    var nColSpan = tableDom.rows[nRow].cells[nCol].colSpan;
                    if (nColSpan > 1) {
                        excelApp.Range(sheet.cells(nRow+1,nExtCol+1),sheet.cells(nRow+1,nExtCol+nColSpan)).select();
                        excelApp.Selection.HorizontalAlignment = 3;
                        excelApp.Selection.MergeCells = true;
                    }
                    sheet.cells(nRow+1,nExtCol+1).value = tableDom.rows(nRow).cells(nCol).innerText;
                    nExtCol=nExtCol+nColSpan;
                }
            }
            loadMarsk.hide();
        };

////////////////////////////////////////////////////////////////////導出到word

var myDate = new Date();
var g_title = "XXXXXXX";
var g_TotalColumn = 14;
var g_arrWidth =  [50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50];

//////////////////////////////////////

// word for VBA constant variables
var wdAdjustSameWidth = 3;
var wdLineStyleSingle = 1;
var wdRowHeightAuto = 0;
var wdLineWidth050pt = 4;
var wdLineWidth075pt = 6;
var wdLineWidth100pt = 8;
var wdLineWidth150pt = 12;
var wdLineWidth300pt = 24;
var wdBorderHorizontal = -5;
var wdBorderVertical = -6;
var wdOrientLandscape=1;//橫向
var wdOrientPortrait=0;//縱向

var wdCharacter = 1;
var wdLine = 5;

function setCellBoarder(cell, wordCellObj) {
    wordCellObj.Borders(1).LineWidth = wdLineWidth150pt;
    wordCellObj.Borders(2).LineWidth = wdLineWidth150pt;
    wordCellObj.Borders(3).LineWidth = wdLineWidth150pt;
    wordCellObj.Borders(4).LineWidth = wdLineWidth150pt;
}

function setCellStyle(cell, selectObj) {
    /*
    for (var nBorder = 1; nBorder <= 4; ++nBorder) {
        with(selectObj.Borders(nBorder)) {
            LineStyle = wdLineStyleSingle;
            LineWidth = wdLineWidth075pt;
        }
    }
    */
    if ( Ext.get(cell).hasClass("cell-top") ) {
        selectObj.Borders(1).LineStyle = wdLineStyleSingle;
        selectObj.Borders(1).LineWidth = wdLineWidth150pt;
    }
    
    if ( Ext.get(cell).hasClass("cell-left") ) {
        selectObj.Borders(2).LineStyle = wdLineStyleSingle;
        selectObj.Borders(2).LineWidth = wdLineWidth150pt;
    }
    
    if ( Ext.get(cell).hasClass("cell-bottom") ) {
        selectObj.Borders(3).LineStyle = wdLineStyleSingle;
        selectObj.Borders(3).LineWidth = wdLineWidth150pt;
    }
    
    if ( Ext.get(cell).hasClass("cell-right") ) {
        selectObj.Borders(4).LineStyle = wdLineStyleSingle;
        selectObj.Borders(4).LineWidth = wdLineWidth150pt;
    }
    
    if ( Ext.get(cell).hasClass("cell-title") ) {
        selectObj.Font.Name = "宋體";
        selectObj.Font.Size = "12";
        selectObj.Font.Bold = true;
    }
    else {
        selectObj.Font.Name = "宋體";
        selectObj.Font.Size = "12";
        selectObj.Font.Bold = false;
    }
    
    if ( Ext.get(cell).hasClass("text-right") ) {
        selectObj.ParagraphFormat.Alignment = 2;
    }
    else if ( Ext.get(cell).hasClass("text-left") ) {
        selectObj.ParagraphFormat.Alignment = 0;
    }
    else {
        selectObj.ParagraphFormat.Alignment = 1;
    }
}

function export2Word() {
    var wordApp, wordDoc, table;
    var myCellColSpan, myCellRowSpan;

    try {
        wordApp = new ActiveXObject("Word.Application");
    } 
    catch (e) {
        alert("無法啓動Word!\n\n" + e.message +
        "\n\n如果您確信您的電腦中已經安裝了Word," +
        "那麼請將本站點添加至IE的可信任站點,並調整可信任站點的安全級別。");
        return false;
    }
    
    wordApp.Visible = true;

    wordDoc = wordApp.Documents.Add();

    // set left margin and right margin
    wordApp.ActiveDocument.PageSetup.LeftMargin = wordApp.CentimetersToPoints(1.5);
    wordApp.ActiveDocument.PageSetup.RightMargin = wordApp.CentimetersToPoints(1.5);
    //設置頁面爲橫向
    wordApp.ActiveDocument.PageSetup.Orientation=wdOrientLandscape
    
    var objSelection = wordApp.Selection;
    with (objSelection) {
        Font.Name = "黑體";
        Font.Size = "22";
        ParagraphFormat.Alignment = 1;
        TypeText(g_title);
        
        TypeParagraph();
        Font.Name = "宋體";
        Font.Size = "12";
    }
    
    // convert table into word
    var tableDom = $("FormEntireTable");
    var nRowCount = tableDom.rows.length;
    
    var myTable = wordDoc.Tables.Add (wordApp.Selection.Range, nRowCount - 1, g_TotalColumn);
    myTable.Select();    // select the entire table
  objSelection.ParagraphFormat.Alignment = 0;
    
    for (var nItem = 0; nItem < g_arrWidth.length; ++nItem) {
        objSelection.Columns.Item(nItem + 1).SetWidth(g_arrWidth[nItem], wdAdjustSameWidth);
    }

    with(objSelection) {
        for (var nBorder = 1; nBorder <= 4; ++nBorder) {
            with(Borders(nBorder)) {
                LineStyle = wdLineStyleSingle;
                LineWidth = wdLineWidth150pt;
            }
        }
        
        with( Borders(wdBorderHorizontal) ) {
            LineStyle = wdLineStyleSingle;
            LineWidth = wdLineWidth075pt;
        }
        
        with( Borders(wdBorderVertical) ) {
            LineStyle = wdLineStyleSingle;
            LineWidth = wdLineWidth075pt;
        }
    }

    // insert table in HTML text content into table in word
    var nSpecRowIndex = -1;
    if ($("tr-insert-before"))
        nSpecRowIndex = $("tr-insert-before").rowIndex;

    var spec_row = 0;
    var spec_row1 = 0;
    var spec_row2 = 0;
    var currow=0;
    var currow1=0;
    var currow2=0;
    spec_row=0;
    for (var nRow = 0; nRow < nRowCount - 1; ++nRow) {
        if (nSpecRowIndex >=0 && nRow == nSpecRowIndex)
            myTable.Cell(nRow + 1, 2).SetHeight(120, wdRowHeightAuto);
        else{
            if(spec_row2==0)
                myTable.Cell(nRow + 1, 2).SetHeight(20, wdRowHeightAuto);
            else
                myTable.Cell(nRow + 1, 7).SetHeight(20, wdRowHeightAuto);
        }
        
        var nColCount = tableDom.rows[nRow].cells.length;
        var nExtCol = 0;
        
        for (var nCol = 0; nCol < nColCount; ++nCol) {
            /*if (nRow == spec_row && g_spec_col.indexOf(nCol) >= 0) {
                nExtCol++;
            }*/
            if(nRow<spec_row && spec_row!=0){
                if(currow!=nRow){
                    nExtCol=1;
                    if(nRow<spec_row1 && spec_row1!=0){
                        if(currow1!=nRow)
                            nExtCol=2;
                            if(nRow<spec_row2 && spec_row2!=0){
                                if(currow2!=nRow)
                                    nExtCol=3;
                            }
                            else
                                spec_row2=0;
                    }
                    else
                        spec_row1=0;
                }
                else{
                    nExtCol=0;
                }
            }
            else
                spec_row=0;
            
            var nColSpan = tableDom.rows[nRow].cells[nCol].colSpan;
            myTable.Cell(nRow + 1, nExtCol + nCol + 1).Select();
        
            if (nColSpan > 1) {
                objSelection.MoveRight(1, nColSpan - 1, 1);
                objSelection.Cells.Merge();
            }
            
            var nRowSpan = tableDom.rows[nRow].cells[nCol].rowSpan;
            myTable.Cell(nRow + 1, nExtCol + nCol + 1).Select();
            if (nRowSpan > 1) {
                if(currow==nRow){
                    if(currow1==nRow){
                        if(spec_row2==0){
                            currow2=nRow;
                            spec_row2=nRowSpan+nRow;
                        }
                    }
                    if(spec_row1==0){
                        currow1=nRow;
                        spec_row1=nRowSpan+nRow;
                    }
                }
                else{
                    currow=nRow;
                    spec_row=nRowSpan+nRow;
                }
                
                objSelection.MoveDown(5, nRowSpan - 1, 1);
                if(spec_row2!=0){
                    objSelection.MoveRight(1, nColSpan - 1, 1);
                }
                objSelection.Cells.Merge();
                
                objSelection.ParagraphFormat.Alignment = 1;
                objSelection.Cells.VerticalAlignment = 1;
            }
            
            setCellStyle(tableDom.rows[nRow].cells[ nCol ], objSelection);

            var strText = tableDom.rows[nRow].cells[ nCol ].innerText;

            objSelection.TypeText(strText);
        }    // end of for (nCol)
    }    // end of for (nRow)
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章