1.OWC的版本,一般有OWC10和OWC11.其中OWC10對應Windows XP Office系列(Office 2003),OWC11對應 Office 2007.並且安裝Office 2007 OWC11並不能兼容 Office 2003 OWC10.所以需要分別安裝
2.應用參考代碼
適用於OWC10和OWC11,並注意對OWC DLL的參考加入引用
using OWC10;
SpreadsheetClass owcSheet = new SpreadsheetClass();
owcSheet.Cells[1, 1] = "標題"//設置整個報表的標題
owcSheet.get_Range(owcSheet.Cells[1, 1], owcSheet.Cells[1, 1]).Font.set_Bold(true);//設置字體加粗
owcSheet.get_Range(owcSheet.Cells[1, 1], owcSheet.Cells[1, 1]).Font.set_Size(22);//設置字體大小
owcSheet.get_Range(owcSheet.Cells[1, 1], owcSheet.Cells[1, colIndex]).set_MergeCells(true);//設置合併單元格
//設置水平居中
owcSheet.get_Range(owcSheet.Cells[1, 1], owcSheet.Cells[1, colIndex]).set_HorizontalAlignment(XlHAlign.xlHAlignCenter);
//设置垂直居中对齐
owcSheet.get_Range(owcSheet.Cells[1, 1], owcSheet.Cells[1, colIndexset_VerticalAlignment(XlVAlign.xlVAlignCenter);
//設置報表最適應寬度
owcSheet.get_Range(owcSheet.Cells[2, 1], owcSheet.Cells[rowSum, colIndex]).Select();
owcSheet.get_Range(owcSheet.Cells[2, 1], owcSheet.Cells[rowSum, colIndex]).Columns.AutoFit();
//設置邊框
owcSheet.get_Range(owcSheet.Cells[1, 1], owcSheet.Cells[rowSum, colIndex]).Borders.set_LineStyle(XlLineStyle.xlContinuous);
owcSheet.get_Range(owcSheet.Cells[1, 1], owcSheet.Cells[rowSum, 1]).Borders[XlBordersIndex.xlEdgeLeft].set_Weight(XlBorderWeight.xlThick);//设置左边线加粗
owcSheet.get_Range(owcSheet.Cells[1, 1], owcSheet.Cells[1, colIndex]).Borders[XlBordersIndex.xlEdgeTop].set_Weight(XlBorderWeight.xlThick);//设置上边线加粗
owcSheet.get_Range(owcSheet.Cells[1, colIndex], owcSheet.Cells[rowSum, colIndex]).Borders[XlBordersIndex.xlEdgeRight].set_Weight(XlBorderWeight.xlThick);//设置右边线加粗
owcSheet.get_Range(owcSheet.Cells[rowSum, 1], owcSheet.Cells[rowSum, colIndex]).Borders[XlBordersIndex.xlEdgeBottom].set_Weight(XlBorderWeight.xlThick);//设置下边线加粗
//輸出整理後的文件
owcSheet.Export(desPathFileName, SheetExportActionEnum.ssExportActionNone, SheetExportFormat.ssExportAsAppropriate);
擴展:
SpreadsheetClass owcSheet = new SpreadsheetClass();
Workbook thisWorkBook = owcSheet.ActiveWorkbook;
Worksheet sheet = null;
//Workbook默認有3個Worksheet
strSheet = "sheet" + Convert.ToString(i+1);
sheet = (Worksheet)thisWorkBook.Worksheets[strSheet];
//取默認的3個之外的Worksheet
sheet = (Worksheet)thisWorkBook.Worksheets.Add(Type.Missing,thisWorkBook.ActiveSheet,Type.Missing,Type.Missing);
sheet.Name= "Sheet名稱";
之後可以使用上述代碼像操作SpreadshhetClass對象一樣往sheet對象中寫入數據,並同樣輸出文件.