最近刚刚写的!呵呵!往已经有的一个EXCEL文件中写入数据! function ExportToexcel(dt as datatable) Dim xlApp As New Excel.Application Dim xlBooks As Excel.Workbooks = xlApp.Workbooks Dim xlSheet As Excel.Worksheet xlBooks.Open(System.Windows.Forms.Application.StartupPath & "\Resport.xls") Dim xlbook As Excel.Workbook = xlBooks.Item(1) xlSheet = xlbook.Worksheets("sheet1") For i As Integer = 0 To dt.Rows.Count - 1 xlSheet.Rows("4:4").select() xlApp.Selection.Insert() For j As Integer = 0 To dt.Columns.Count - 1 xlSheet.Cells(4, j + 1) = dt.Rows(i).ItemArray(j) Next Next xlSheet.Cells(1, 1) = ReportTitle.Text xlSheet.Pictures.Insert(System.Windows.Forms.Application.StartupPath & "\~conttempReportPictUre.jpg").Select() xlApp.Selection.ShapeRange.IncrementLeft(250) xlApp.Selection.ShapeRange.IncrementTop(250) xlApp.Selection.ShapeRange.Height = 280 xlApp.Selection.ShapeRange.Width = 330 'Kill(System.Windows.Forms.Application.StartupPath & "\~conttempReportPictUre.jpg") xlbook.SaveAs(Savefilename) xlbook.Saved = True xlbook.Close() xlBooks.Close() xlApp.Quit() xlbook = Nothing xlBooks = Nothing xlApp = Nothing GC.Collect()end function
oWorkBook = oExcel.Workbooks.Add(filename);
我们一般采用以上方法打开Excel。
你的问题是不同的office版本,提供控件的接口参数个数不同,比如Excel2000的save参数是11个,而Excel2003的Save参数是12个,查看一下你机器上的office开发帮助
能具体点吗
我的office版本是XP版本的会有问题码??
还有就是用oWorkBook = oExcel.Workbooks.Add(filename);
方法能完成我上面的要求码(利用模板来格式输出)
请大师指点
http://community.csdn.net/Expert/topic/3115/3115187.xml?temp=.6487085
function ExportToexcel(dt as datatable)
Dim xlApp As New Excel.Application
Dim xlBooks As Excel.Workbooks = xlApp.Workbooks Dim xlSheet As Excel.Worksheet
xlBooks.Open(System.Windows.Forms.Application.StartupPath & "\Resport.xls") Dim xlbook As Excel.Workbook = xlBooks.Item(1)
xlSheet = xlbook.Worksheets("sheet1")
For i As Integer = 0 To dt.Rows.Count - 1
xlSheet.Rows("4:4").select()
xlApp.Selection.Insert()
For j As Integer = 0 To dt.Columns.Count - 1 xlSheet.Cells(4, j + 1) = dt.Rows(i).ItemArray(j) Next Next xlSheet.Cells(1, 1) = ReportTitle.Text xlSheet.Pictures.Insert(System.Windows.Forms.Application.StartupPath & "\~conttempReportPictUre.jpg").Select()
xlApp.Selection.ShapeRange.IncrementLeft(250)
xlApp.Selection.ShapeRange.IncrementTop(250)
xlApp.Selection.ShapeRange.Height = 280
xlApp.Selection.ShapeRange.Width = 330
'Kill(System.Windows.Forms.Application.StartupPath & "\~conttempReportPictUre.jpg")
xlbook.SaveAs(Savefilename)
xlbook.Saved = True
xlbook.Close()
xlBooks.Close()
xlApp.Quit()
xlbook = Nothing
xlBooks = Nothing
xlApp = Nothing
GC.Collect()end function
能具体点吗”
××××××××
就是将open方法中的missing,全部替换为Type.Missing。
int rowIndex=1;
int colIndex=0; excel.Application.Workbooks.Add(true);
DataTable table=GetData();
//将所得到的表的列名,赋值给单元格
foreach(DataColumn col in table.Columns)
{
colIndex++;
excel.Cells[1,colIndex]=col.ColumnName;
} //同样方法处理数据
foreach(DataRow row in table.Rows)
{
rowIndex++;
colIndex=0;
foreach(DataColumn col in table.Columns)
{
colIndex++;
excel.Cells[rowIndex,colIndex]=row[col.ColumnName].ToString();
}
}
//不可见,即后台处理
excel.Visible=true;
using XXXXX?