唉,自己搞定了 1.先创建一个DataTable,作为数据来源,也可以另将其它的数据源。 Private Function CreaTable() As DataTable Dim dt As New DataTable() dt.Columns.Add("列1", GetType(String)) dt.Columns.Add("列2", GetType(Integer)) dt.Columns.Add("列3", GetType(String)) dt.Columns.Add("列4", GetType(String)) Dim row, row1 As DataRow row = dt.NewRow() row!列1 = "行1" row!列2 = 1 row!列3 = "d" row!列4 = "a" dt.Rows.Add(row) row1 = dt.NewRow() row1!列1 = "行2" row1!列2 = 12 row1!列3 = "b" row1!列4 = "c" dt.Rows.Add(row1) Return dt End Function 2.将表中的内容导出到Excel Dim xlApp As New Excel.Application() Dim xlBook As Excel.Workbook Dim xlSheet As Excel.Worksheet Dim rowIndex, colIndex As Integer rowIndex = 1 colIndex = 0 xlBook = xlApp.Workbooks().Add xlSheet = xlBook.Worksheets("sheet1") Dim Table As New DataTable() Table = CreaTable() '将所得到的表的列名,赋值给单元格 Dim Col As DataColumn Dim Row As DataRow For Each Col In Table.Columns colIndex = colIndex + 1 xlApp.Cells(1, colIndex) = Col.ColumnName Next '得到的表所有行,赋值给单元格 For Each Row In Table.Rows rowIndex = rowIndex + 1 colIndex = 0 For Each Col In Table.Columns colIndex = colIndex + 1 xlApp.Cells(rowIndex, colIndex) = Row(Col.ColumnName) Next Next With xlSheet .Range(.Cells(1, 1), .Cells(1, colIndex)).Font.Name = "黑体" '设标题为黑体字 .Range(.Cells(1, 1), .Cells(1, colIndex)).Font.Bold = True '标题字体加粗 .Range(.Cells(1, 1), .Cells(rowIndex, colIndex)).Borders.LineStyle = 1 '设表格边框样式 End With With xlSheet.PageSetup .LeftHeader = "" & Chr(10) & "&""楷体_GB2312,常规""&10公司名称:" ' & Gsmc .CenterHeader = "&""楷体_GB2312,常规""公司人员情况表&""宋体,常规""" & Chr(10) & "&""楷体_GB2312,常规""&10日 期:" .RightHeader = "" & Chr(10) & "&""楷体_GB2312,常规""&10单位:" .LeftFooter = "&""楷体_GB2312,常规""&10制表人:" .CenterFooter = "&""楷体_GB2312,常规""&10制表日期:" .RightFooter = "&""楷体_GB2312,常规""&10第&P页 共&N页" End With xlApp.Visible = True 3.将表中的内容导出到WORD Dim wordApp As New Word.Application() Dim myDoc As Word.Document Dim oTable As Word.Table Dim rowIndex, colIndex As Integer rowIndex = 1 colIndex = 0 wordApp.Documents.Add() myDoc = wordApp.ActiveDocument Dim Table As New DataTable() Table = CreaTable() oTable = myDoc.Tables.Add(Range:=myDoc.Range(Start:=0, End:=0), NumRows:=Table.Rows.Count + 1, NumColumns:=Table.Columns.Count) '将所得到的表的列名,赋值给单元格 Dim Col As DataColumn Dim Row As DataRow For Each Col In Table.Columns colIndex = colIndex + 1 oTable.Cell(1, colIndex).Range.InsertAfter(Col.ColumnName) Next '得到的表所有行,赋值给单元格 For Each Row In Table.Rows rowIndex = rowIndex + 1 colIndex = 0 For Each Col In Table.Columns colIndex = colIndex + 1 oTable.Cell(rowIndex, colIndex).Range.InsertAfter(Row(Col.ColumnName)) Next Next oTable.Borders.InsideLineStyle = 1 oTable.Borders.OutsideLineStyle = 1 wordApp.Visible = True
<html> <head> <title></title><script language="JavaScript" type="text/JavaScript"> <!-- var OutputString;function julianDay( Y, M, D ){
var y,m,d;
// Guard against arguments that are really passed as strings: y=parseInt(Y); m=parseInt(M); d=parseInt(D);
var jy,ja,jm;
// Don't permit year zero: it does not exist in the Julian calendar: if( y==0 ){ alert("There was no year zero in the Julian calendar."); return "invalid"; }
// Don't permit the missing 10 days in October of 1582: if( y==1582 && m==10 && d>4 && d<15 ){ alert("October 5th to 14th were skipped in October of 1582\nwhen Pope Gregory XIII implemented the calendar reforms\nof the commission headed by the Jesuit mathematician and astronomer\nChristoph Clavius."); return "invalid"; }
// Negative years represent years before the Common Era (BCE). // Correct for the fact that there was no year zero: if(y<0) y++; if(m>2){ jy=y; jm=m+1; }else{ jy=y-1; jm=m+13; }
var julian = Math.floor( Math.floor(365.25*jy) + Math.floor(30.6001*jm) + d + 1720995 );
// Check for the switch to the Gregorian calendar: var gregorianStart = 15 + 31*( 10 + 12*1582 );
if( d + 31*(m + 12*y) >= gregorianStart ){ ja = Math.floor(0.01*jy); julian += 2 - ja + Math.floor(0.25*ja); }
// return the Julian day number: return julian;
}function julianGregorianDate( julian ){
var j1, j2, j3, j4, j5;
// This is of course the Julian day number // for October 15, 1582: var gregorianStart = 2299161; if( julian >= gregorianStart ){ var tmp = Math.floor( ( (julian - 1867216) - 0.25 ) / 36524.25 ); j1 = julian + 1 + tmp - Math.floor(0.25*tmp); }else{ j1 = julian; }
var d = Math.floor(j2 - j4 - Math.floor(j5*30.6001)); var m = Math.floor(j5 - 1); if( m > 12 ) m -= 12; var y = Math.floor(j3 - 4715); if( m > 2 ) y--; if( y <= 0 ) y--;
//var dateString=document.getElementById( objectId ).value; // Normalize possibly variant delimiters to the ISO preferred delimiter: objectId = 'theDate'; var re = new RegExp( '[/\.\ ]', 'gi' ); dateString=dateString.replace( re, "-" ); // Split the date string: dateComponents=dateString.split("-"); var year,month,day; year=dateComponents[0]; month=dateComponents[1]; day=dateComponents[2]; generateCalendar( year, month, day, objectId );
}// // setDate() // function setDate(day){
// Format the date string: year =document.getElementById("currentYear").value; month=document.getElementById("currentMonth").value; isoDateString=formatISODate(year,month,day); // Get the destination object id: idString = document.getElementById("destinationId").value; // Store the value: //window.opener.document.getElementById( idString ).value=isoDateString; //window.close();
}// // previousYear() // function previousYear(){
year =parseInt(document.getElementById("currentYear").value); month=parseInt(document.getElementById("currentMonth").value); day =parseInt(document.getElementById("currentDay").value); destination = document.getElementById("destinationId").value; year -= 1; // Remember that there was no year zero: if( year==0 ) year = -1; // // NOTE BENE: The generateCalendar() function *MUST* be called // from the "window.opener" window. Otherwise, if you just call // "generateCalendar()" from *this* window, then the "opener" // property becomes the calendar popup window itself in the new // incarnation of the calendar window -- instead of being // the form window -- and then you would be stuck with no // way to access the form elements in the main form window! // //window.opener.generateCalendar(year,month,day,destination); }// // previousMonth() // function previousMonth(){
year =parseInt(document.getElementById("currentYear").value); month=parseInt(document.getElementById("currentMonth").value); day =parseInt(document.getElementById("currentDay").value); destination = document.getElementById("destinationId").value; month -= 1; if(month<1){ month = 12; year -= 1; if( year==0 ) year = -1; } //window.opener.generateCalendar(year,month,day,destination); }
// // nextMonth() // function nextMonth(){
year =parseInt(document.getElementById("currentYear").value); month=parseInt(document.getElementById("currentMonth").value); day =parseInt(document.getElementById("currentDay").value); destination = document.getElementById("destinationId").value; month += 1; if(month>12){ month = 1; year += 1; if( year==0 ) year = 1; } //window.opener.generateCalendar(year,month,day,destination); }// // nextYear() // function nextYear(){
year =parseInt(document.getElementById("currentYear").value); month=parseInt(document.getElementById("currentMonth").value); day =parseInt(document.getElementById("currentDay").value); destination = document.getElementById("destinationId").value; year += 1; // Remember that there was no year zero: if( year==0 ) year = 1; //window.opener.generateCalendar(year,month,day,destination); }function generateCalendar( Y, M, D, targetId ){
// Names of the Months: //var Month = new Array("January","February","March","April","May","June","July","August","September","October","November","December"); var Month = new Array("1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"); // Days of the Week: //var Day = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat"); var Day = new Array("星期日","星期一","星期二","星期三","星期四","星期五","星期六"); // Digits (if you want to use something other than Arabic-Indic digits): // var Digits = new Array("0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31");
var daysInMonth = new Array(31,28,31,30,31,30,31,31,30,31,30,31); var daysInAWeek=7;
// Guard against arguments that are really passed as strings: // Get year: var today = new Date(); if( Y=="" || isNaN(Y) ){ thisYear=today.getFullYear(); }else{ thisYear=parseInt(Y); } // Get month: if( M=="" || isNaN(M) ){ thisMonth=today.getMonth()+1; }else{ thisMonth=parseInt(M); if(thisMonth<1) thisMonth=1; if(thisMonth>12) thisMonth=12; } // Get day: if( D=="" || isNaN(D) ){ thisDay=today.getDate(); }else{ thisDay=parseInt(D); if(thisDay<0) thisDay=1; if(thisDay>31) thisDay=31; }
// Calculate the number of days in February: if ((thisYear % 4) == 0){ daysInMonth[ 1 ] = 29; if((thisYear % 100) == 0 && (thisYear % 400) != 0){ daysInMonth[ 1 ] = 28; } }
<head>
<title></title><script language="JavaScript" type="text/JavaScript">
<!--
var OutputString;function julianDay( Y, M, D ){
var y,m,d;
// Guard against arguments that are really passed as strings:
y=parseInt(Y);
m=parseInt(M);
d=parseInt(D);
var jy,ja,jm;
// Don't permit year zero: it does not exist in the Julian calendar:
if( y==0 ){
alert("There was no year zero in the Julian calendar.");
return "invalid";
}
// Don't permit the missing 10 days in October of 1582:
if( y==1582 && m==10 && d>4 && d<15 ){
alert("October 5th to 14th were skipped in October of 1582\nwhen Pope Gregory XIII implemented the calendar reforms\nof the commission headed by the Jesuit mathematician and astronomer\nChristoph Clavius.");
return "invalid";
}
// Negative years represent years before the Common Era (BCE).
// Correct for the fact that there was no year zero:
if(y<0) y++; if(m>2){
jy=y;
jm=m+1;
}else{
jy=y-1;
jm=m+13;
}
var julian = Math.floor( Math.floor(365.25*jy) + Math.floor(30.6001*jm) + d + 1720995 );
// Check for the switch to the Gregorian calendar:
var gregorianStart = 15 + 31*( 10 + 12*1582 );
if( d + 31*(m + 12*y) >= gregorianStart ){
ja = Math.floor(0.01*jy);
julian += 2 - ja + Math.floor(0.25*ja);
}
// return the Julian day number:
return julian;
}function julianGregorianDate( julian ){
var j1, j2, j3, j4, j5;
// This is of course the Julian day number
// for October 15, 1582:
var gregorianStart = 2299161;
if( julian >= gregorianStart ){
var tmp = Math.floor( ( (julian - 1867216) - 0.25 ) / 36524.25 );
j1 = julian + 1 + tmp - Math.floor(0.25*tmp);
}else{
j1 = julian;
}
j2 = j1 + 1524;
j3 = Math.floor( 6680.0 + ( (j2 - 2439870) - 122.1 )/365.25 );
j4 = Math.floor(j3*365.25);
j5 = Math.floor( (j2 - j4)/30.6001 );
var d = Math.floor(j2 - j4 - Math.floor(j5*30.6001));
var m = Math.floor(j5 - 1);
if( m > 12 ) m -= 12;
var y = Math.floor(j3 - 4715);
if( m > 2 ) y--;
if( y <= 0 ) y--;
return formatISODate(y,m,d);
}function dayOfWeek( julian ){
return (julian+1)%7;
}function padWithZeros(number,length) {
var str = "" + number;
while( str.length < length ) str = '0' + str;
return str;
}
// Date delimiter:
var delimiter="-";
if(year<0){
year= -year; year = "-" + padWithZeros(year,4);
}else{
year = padWithZeros(year,4);
}
return year + delimiter + padWithZeros(month,2) + delimiter + padWithZeros(day,2);
}function callCalendarPlan()
{
var today = new Date();
objectId = 'theDate';
year=today.getFullYear();
month=today.getMonth()+1;
day=today.getDate();
generateCalendar( year, month, day, objectId );
}function callCalendar( dateString ){
//var dateString=document.getElementById( objectId ).value;
// Normalize possibly variant delimiters to the ISO preferred delimiter:
objectId = 'theDate';
var re = new RegExp( '[/\.\ ]', 'gi' );
dateString=dateString.replace( re, "-" );
// Split the date string:
dateComponents=dateString.split("-");
var year,month,day;
year=dateComponents[0];
month=dateComponents[1];
day=dateComponents[2];
generateCalendar( year, month, day, objectId );
}//
// setDate()
//
function setDate(day){
// Format the date string:
year =document.getElementById("currentYear").value;
month=document.getElementById("currentMonth").value;
isoDateString=formatISODate(year,month,day);
// Get the destination object id:
idString = document.getElementById("destinationId").value;
// Store the value:
//window.opener.document.getElementById( idString ).value=isoDateString;
//window.close();
}//
// previousYear()
//
function previousYear(){
year =parseInt(document.getElementById("currentYear").value);
month=parseInt(document.getElementById("currentMonth").value);
day =parseInt(document.getElementById("currentDay").value);
destination = document.getElementById("destinationId").value;
year -= 1;
// Remember that there was no year zero:
if( year==0 ) year = -1;
//
// NOTE BENE: The generateCalendar() function *MUST* be called
// from the "window.opener" window. Otherwise, if you just call
// "generateCalendar()" from *this* window, then the "opener"
// property becomes the calendar popup window itself in the new
// incarnation of the calendar window -- instead of being
// the form window -- and then you would be stuck with no
// way to access the form elements in the main form window!
//
//window.opener.generateCalendar(year,month,day,destination);
}//
// previousMonth()
//
function previousMonth(){
year =parseInt(document.getElementById("currentYear").value);
month=parseInt(document.getElementById("currentMonth").value);
day =parseInt(document.getElementById("currentDay").value);
destination = document.getElementById("destinationId").value;
month -= 1;
if(month<1){
month = 12;
year -= 1;
if( year==0 ) year = -1;
}
//window.opener.generateCalendar(year,month,day,destination);
}
// nextMonth()
//
function nextMonth(){
year =parseInt(document.getElementById("currentYear").value);
month=parseInt(document.getElementById("currentMonth").value);
day =parseInt(document.getElementById("currentDay").value);
destination = document.getElementById("destinationId").value;
month += 1;
if(month>12){
month = 1;
year += 1;
if( year==0 ) year = 1;
}
//window.opener.generateCalendar(year,month,day,destination);
}//
// nextYear()
//
function nextYear(){
year =parseInt(document.getElementById("currentYear").value);
month=parseInt(document.getElementById("currentMonth").value);
day =parseInt(document.getElementById("currentDay").value);
destination = document.getElementById("destinationId").value;
year += 1;
// Remember that there was no year zero:
if( year==0 ) year = 1;
//window.opener.generateCalendar(year,month,day,destination);
}function generateCalendar( Y, M, D, targetId ){
// Names of the Months:
//var Month = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
var Month = new Array("1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月");
// Days of the Week:
//var Day = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
var Day = new Array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
// Digits (if you want to use something other than Arabic-Indic digits):
// var Digits = new Array("0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31");
var daysInMonth = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
var daysInAWeek=7;
// Guard against arguments that are really passed as strings:
// Get year:
var today = new Date();
if( Y=="" || isNaN(Y) ){
thisYear=today.getFullYear();
}else{
thisYear=parseInt(Y);
}
// Get month:
if( M=="" || isNaN(M) ){
thisMonth=today.getMonth()+1;
}else{
thisMonth=parseInt(M);
if(thisMonth<1) thisMonth=1;
if(thisMonth>12) thisMonth=12;
}
// Get day:
if( D=="" || isNaN(D) ){
thisDay=today.getDate();
}else{
thisDay=parseInt(D);
if(thisDay<0) thisDay=1;
if(thisDay>31) thisDay=31;
}
// Calculate the number of days in February:
if ((thisYear % 4) == 0){
daysInMonth[ 1 ] = 29;
if((thisYear % 100) == 0 && (thisYear % 400) != 0){
daysInMonth[ 1 ] = 28;
}
}
// Document header:
OutputString = " ";
// Start the calendar table with day headings:
OutputString += "<table border='0' width='100%' cellpadding='4' cellspacing='1'>\n <tr>\n";
OutputString += " <td bgcolor='#f7f3f7' align='right'><div class=\"cssleftstretcharrow\" onclick=\"previousYear()\">上年<!-- left arrow --></div></td>\n";
OutputString += " <td bgcolor='#f7f3f7'><div class=\"cssleftarrow\" onclick=\"previousMonth()\">上月<!-- left arrow --></div></td>\n"; // Print the year heading: If year<0, negate and print "BCE"; if 0<year<1000, print "CE":
OutputString += " <td bgcolor='#f7f3f7' colspan=\"3\" align='center'><font color='#6600FF'><B>";
if( thisYear < 0 ) yearString = -thisYear + " BCE";
else if( thisYear < 1000 ) yearString = thisYear + " CE";
else yearString = thisYear;
OutputString += yearString + "年\n";
// Print the month heading:
OutputString += Month[ thisMonth-1 ] + "</B></font></td>"; OutputString += " <td bgcolor='#f7f3f7' align='right'><div class=\"cssrightarrow\" onclick=\"nextMonth()\">下月<!-- right arrow --></div></td>\n";
OutputString += " <td bgcolor='#f7f3f7'><nobr><div class=\"cssrightstretcharrow\" onclick=\"nextYear()\">下年<!-- right arrow --></div></nobr></td>\n";
OutputString += "</tr>\n<tr width='2' bgcolor='#000066'>\n<td colspan='7'>\n</td>\n</tr>\n<tr width='20'>\n<td bgcolor='#f7f3f7' colspan='7'>\n</td>\n</tr>\n<tr>\n";
for(i=0;i<daysInAWeek;i++){
OutputString += " <td bgcolor='#f7f3f7' align='center'>" + Day[i] + "</td>\n";
}
OutputString += " </tr>\n <tr>\n";