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

猜你喜欢

转载自blog.csdn.net/vera514514/article/details/9063853
今日推荐