Domino導出excel

Sub Initialize
 On Error Goto e
 Dim se As New NotesSession
 Dim db As NotesDatabase
 Dim view As NotesView
 Dim doc As NotesDocument
 Set db = se.CurrentDatabase
 Set view = db.GetView("showRTF")
 Set doc = view.GetFirstDocument

 Print "<SCRIPT LANGUAGE='JavaScript'>"
 Print | var xls = new ActiveXObject('Excel.Application');        
 xls.visible =true;  //設置excel爲可見
 var xlBook = xls.Workbooks.Add;
 var xlsheet = xlBook.Worksheets(1);

    <!--合併第一行的第一列到第五列-->
      xlsheet.Range(xlsheet.Cells(1,1),xlsheet.Cells(1,5)).mergecells=true;
      xlsheet.Range(xlsheet.Cells(1,1),xlsheet.Cells(1,5)).value="人員信息採樣表";
 '有時候大家發現有好多表格合併啊,拆分啊等等很難處理,在程序裏不知道如何做,但是經本人仔細思考後發現,只要上面這2行好好利用,加上notesview的一些類,想要什麼樣的表格就有什麼樣的表格,例如這2行代碼如果放到函數的結尾處。。。。。等主程序都執行完了再來執行這2行那。。。。是不是你也明白吧。我可是想了好幾天後才發現的。

    <!--設置行高-->
    xlsheet.Rows(1).RowHeight = 25;
    xlsheet.Rows(1).Font.Size=14;
    xlsheet.Rows(1).Font.Name="黑體";

    <!--設置顯示字符而不是數字-->
     //設置單元格內容自動換行 range.WrapText  =  true  ;
     //設置標題欄
     xlsheet.Cells(2,1).Value="公司名稱";
     xlsheet.Cells(2,2).Value="部門名稱";
     xlsheet.Cells(2,3).Value="科室名稱";
     xlsheet.Cells(2,4).Value="員工名稱";
     xlsheet.Cells(2,5).Value="職務";|
 Dim i As Integer
 i=3
 '--下面的大循環用不同的方法會有意向不到的效果,例如notesviewnavgator類,例如用notesviewcollection類,例如用notesviewEntry,這三種類對你的處理簡直是如虎添翼,剛開始我只用下面的方法發現好多問題不能很好的處理,後來翻看這幾個類時恍然大悟。
 While Not doc Is Nothing

  Dim RTFitem As NotesRichTextItem
  Dim PeoplesArrary As String
  '下面是做過的項目中其中一個處理RTF域的內容的實例,
  Set RTFitem = doc.GetFirstItem("RTFstr")               '獲得RTF域,
  PeoplesArrary=RTFitem.GetUnFormattedText()      '這樣得到的RTF域的值加入數組後不會有多餘的空格,如果用
                                                                                   'GetFormattedText()會有莫名去不掉的空格。

  '--如果是RTF域的內容一定要處理,這裏RTF域裏只有文本,沒有圖片和附件。
  PeoplesArrary=Replace(PeoplesArrary," ","")
  PeoplesArrary=Replace(PeoplesArrary,";",";")
  PeoplesArrary=Replace(PeoplesArrary,Chr(10),"")
  PeoplesArrary=Replace(PeoplesArrary,Chr(10)+Chr(13),"")
  PeoplesArrary=Replace(PeoplesArrary,Chr(13),"")    

  Print |xlsheet.Cells(|+Cstr(i)+|,1).ColumnWidth = '80';|

 '設置列寬,必須和自動換行一起使用才其作用。經本人測試通過。想了好幾天,奶奶的。
  Print |xlsheet.Cells(|+Cstr(i)+|,1).WrapText=true;|       '自動換行
  Print |xlsheet.Cells(|+Cstr(i)+|,1).Value="|+PeoplesArrary+|";|

  i = i +1
  Set doc = view.GetNextDocument(doc)
 Wend

 Print |xlsheet.Columns.AutoFit;|
 Print |xls.UserControl = true;  //很重要,不能省略,不然會出問題 意思是excel交由用戶控制|
 Print |xls=null;|
 Print |xlBook=null;|
 Print |xlsheet=null;|
 Print |window.opener=null;|
 Print |window.open('','_self');|
 Print |window.close();|
 Print |</script>|
 e:
 Msgbox Cstr(Error)+"----------------"+Cstr(Erl)
  End Sub

當然還有很多參數可以合理利用,其中的JS參數設置在下面貼出,想用什麼,直接用就行。

1創建

var XLObj = new ActiveXObject("Excel.Application" );
var xlBook = XLObj.Workbooks.Add; //新增工作簿
var ExcelSheet = xlBook.Worksheets(1); //創建工作表

2.保存表格

ExcelSheet.SaveAs("C:\\TEST.XLS" );

3.使 Excel 通過 Application 對象可見

ExcelSheet.Application.Visible = true;

4.打印

xlBook.PrintOut;
或者:
ExcelSheet.PrintOut;

5.關閉

xlBook.Close(savechanges=false);
或者:
ExcelSheet.Close(savechanges=false);

6.結束進程

ExcelSheet.Application.Quit();
或者:
XLObj.Quit();
XLObj=null;

7.頁面設置

ExcelSheet.ActiveSheet.PageSetup.LeftMargin= 2/0.035;
//頁邊距 左2釐米
ExcelSheet.ActiveSheet.PageSetup.RightMargin = 3/0.035;
//頁邊距右3釐米
ExcelSheet.ActiveSheet.PageSetup.TopMargin = 4/0.035;
//頁邊距上4釐米
ExcelSheet.ActiveSheet.PageSetup.BottomMargin = 5/0.035;
//頁邊距下5釐米
ExcelSheet.ActiveSheet.PageSetup.HeaderMargin = 1/0.035;
//頁邊距頁眉1釐米
ExcelSheet.ActiveSheet.PageSetup.FooterMargin = 2/0.035;
//頁邊距頁腳2釐米
ExcelSheet.ActiveSheet.PageSetup.CenterHeader = "頁眉中部內容";
ExcelSheet.ActiveSheet.PageSetup.LeftHeader = "頁眉左部內容";
ExcelSheet.ActiveSheet.PageSetup.RightHeader = "頁眉右部內容";
ExcelSheet.ActiveSheet.PageSetup.CenterFooter = "頁腳中部內容";
ExcelSheet.ActiveSheet.PageSetup.LeftFooter = "頁腳左部內容";
ExcelSheet.ActiveSheet.PageSetup.RightFooter = "頁腳右部內容";

8.對單元格操作,帶*部分對於行,列,區域都有相應屬性

ExcelSheet.ActiveSheet.Cells(row,col).Value = "內容";
//設置單元格內容
ExcelSheet.ActiveSheet.Cells(row,col).Borders.Weight = 1;
//設置單元格邊框*()
ExcelSheet.ActiveSheet.Cells(row,col).Interior.ColorIndex = 1;
//設置單元格底色*(1-黑色,2-白色,3-紅色,4-綠色,5-藍色,6-黃色,7-粉紅色,8-天藍色,9-醬土色..可以多做嘗試)
ExcelSheet.ActiveSheet.Cells(row,col).Interior.Pattern = 1;
//設置單元格背景樣式*(1-無,2-細網格,3-粗網格,4-斑點,5-橫線,6-豎線..可以多做嘗試)
ExcelSheet.ActiveSheet.Cells(row,col).Font.ColorIndex = 1;
//設置字體顏色*(與上相同)
ExcelSheet.ActiveSheet.Cells(row,col).Font.Size = 10;
//設置爲10號字*
ExcelSheet.ActiveSheet.Cells(row,col).Font.Name = "黑體";
//設置爲黑體*
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

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