JS Excel knowledge points
1. Create var XLObj = new ActiveXObject("Excel.Application" ); var xlBook = XLObj.Workbooks.Add; //Add workbook var ExcelSheet = xlBook.Worksheets(1); //Create worksheet 2. Save the table ExcelSheet.SaveAs("C:\\TEST.XLS" ); 3. Make Excel visible through the Application object ExcelSheet.Application.Visible = true; 4. Print xlBook.PrintOut; or: ExcelSheet.PrintOut; 5. Close xlBook.Close (savechanges=false); or: ExcelSheet.Close(savechanges=false); 6. End the process ExcelSheet.Application.Quit(); or: XLObj.Quit(); XLObj=null; 7. Page Setup ExcelSheet.ActiveSheet.PageSetup .LeftMargin= 2/0.035; margin left 2 cm
ExcelSheet.ActiveSheet.PageSetup.RightMargin = 3/0.035;
margin right 3 cm
ExcelSheet.ActiveSheet.PageSetup.TopMargin = 4/0.035;
margin top 4 cm
ExcelSheet.ActiveSheet.PageSetup.BottomMargin = 5/0.035;
margin Bottom 5cm
ExcelSheet.ActiveSheet.PageSetup.HeaderMargin = 1/0.035;
Margin Header 1cm
ExcelSheet.ActiveSheet.PageSetup.FooterMargin = 2/0.035;
Margin Footer 2cm
ExcelSheet.ActiveSheet.PageSetup.CenterHeader = "Header middle content";
ExcelSheet.ActiveSheet.PageSetup.LeftHeader = "Header left content";
ExcelSheet.ActiveSheet.PageSetup.RightHeader = "Header right content";
ExcelSheet.ActiveSheet.PageSetup.CenterFooter = "Footer Middle content";
ExcelSheet.ActiveSheet.PageSetup.LeftFooter = "Footer left content";
ExcelSheet.ActiveSheet.PageSetup.RightFooter = "Content on the right side of the footer";
8. For cell operations, the part with * has corresponding properties for rows, columns and areas ExcelSheet.ActiveSheet.Cells(row,col).Value = " Content"; set cell content ExcelSheet.ActiveSheet.Cells(row,col).Borders.Weight = 1; set cell border() ExcelSheet.ActiveSheet.Cells(row, col).Interior.ColorIndex = 1; set cell Background color (1-black, 2-white, 3-red, 4-green, 5-blue, 6-yellow, 7-pink, 8-sky blue, 9-sauce color.. You can try more) ExcelSheet.ActiveSheet.Cells(row,col).Interior.Pattern = 1; set cell background style *(1-none, 2-fine grid, 3-coarse grid, 4-spot, 5-horizontal, 6 - Vertical line.. You can try more) ExcelSheet.ActiveSheet.Cells(row,col).Font.ColorIndex = 1; Set the font color (same as above) ExcelSheet.ActiveSheet.Cells(row,col).Font.Size = 10; set to 10 size ExcelSheet.ActiveSheet.Cells(row,col).Font.Name = "bold"; set to bold
ExcelSheet.ActiveSheet.Cells(row,col).Font.Italic = true;
设置为斜体
ExcelSheet.ActiveSheet.Cells(row,col).Font.Bold = true;
设置为粗体
ExcelSheet.ActiveSheet.Cells(row,col).ClearContents;
清除内容
ExcelSheet.ActiveSheet.Cells(row,col).WrapText=true;
设置为自动换行
ExcelSheet.ActiveSheet.Cells(row,col).HorizontalAlignment = 3;
水平对齐方式枚举* (1-常规,2-靠左,3-居中,4-靠右,5-填充 6-两端对齐,7-跨列居中,8-分散对齐)
ExcelSheet.ActiveSheet.Cells(row,col).VerticalAlignment = 2;
垂直对齐方式枚举*(1-靠上,2-居中,3-靠下,4-两端对齐,5-分散对齐)
行,列有相应操作:
ExcelSheet.ActiveSheet.Rows(row).
ExcelSheet.ActiveSheet.Columns(col).
ExcelSheet.ActiveSheet.Rows(startrow+":"+endrow).
如Rows("1:5" )即1到5行
ExcelSheet.ActiveSheet.Columns(startcol+":"+endcol).
如Columns("1:5" )即1到5列
区域有相应操作:
XLObj.Range(startcell+":"+endcell).Select;
如Range("A2:H8" )即A列第2格至H列第8格的整个区域
XLObj.Selection.
合并单元格
XLObj.Range(startcell+":"+endcell).MergeCells = true;
如Range("A2:H8" )即将A列第2格至H列第8格的整个区域合并为一个单元格
XLObj.Range("A2",XLObj.Cells(8,8)).MergeCells = true;
9.设置行高与列宽
ExcelSheet.ActiveSheet.Columns(startcol+":"+endcol).ColumnWidth = 22;
设置从firstcol到stopcol列的宽度为22
ExcelSheet.ActiveSheet.Rows(startrow+":"+endrow).RowHeight = 22;
设置从firstrow到stoprow行的宽度为22