您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 管理学资料 > C#操作Excel的类
C#操作Excel的类(上)2008-11-2722:32usingSystem;usingMicrosoft.Office.Core;usingExcel;namespaceWebExcell1{///summary///Class1的摘要说明。////summaryclassExcelEdit{stringmyFileName;Excel.ApplicationmyExcel;Excel.WorkbookmyWorkBook;publicstringMyFileName{get{returnmyFileName;}set{myFileName=value;}}///summary///构造函数,不创建Excel工作薄////summarypublicExcelEdit(){//请不要删除以下信息//版权:}///summary///创建Excel工作薄////summarypublicvoidCreateExcel(){myExcel=newExcel.Application();myWorkBook=myExcel.Application.Workbooks.Add(true);}///summary///显示Excel////summarypublicvoidShowExcel(){myExcel.Visible=true;}///summary///将数据写入Excel////summary///paramname=data要写入的二维数组数据/param///paramname=startRowExcel中的起始行/param///paramname=startColumnExcel中的起始列/parampublicvoidWriteData(string[,]data,intstartRow,intstartColumn){introwNumber=data.GetLength(0);intcolumnNumber=data.GetLength(1);for(inti=0;irowNumber;i++){for(intj=0;jcolumnNumber;j++){//在Excel中,如果某单元格以单引号“'”开头,表示该单元格为纯文本,因此,我们在每个单元格前面加单引号。myExcel.Cells[startRow+i,startColumn+j]='+data[i,j];}}}///summary///将数据写入Excel////summary///paramname=data要写入的字符串/param///paramname=starRow写入的行/param///paramname=startColumn写入的列/parampublicvoidWriteData(stringdata,introw,intcolumn){myExcel.Cells[row,column]=data;}///summary///将数据写入Excel////summary///paramname=data要写入的数据表/param///paramname=startRowExcel中的起始行/param///paramname=startColumnExcel中的起始列/parampublicvoidWriteData(System.Data.DataTabledata,intstartRow,intstartColumn){for(inti=0;i=data.Rows.Count-1;i++){for(intj=0;j=data.Columns.Count-1;j++){//在Excel中,如果某单元格以单引号“'”开头,表示该单元格为纯文本,因此,我们在每个单元格前面加单引号。myExcel.Cells[startRow+i,startColumn+j]='+data.Rows[i][j].ToString();}}}///summary///读取指定单元格数据////summary///paramname=row行序号/param///paramname=column列序号/param///returns该格的数据/returnspublicstringReadData(introw,intcolumn){Excel.Rangerange=myExcel.get_Range(myExcel.Cells[row,column],myExcel.Cells[row,column]);returnrange.Text.ToString();}///summary///向Excel中插入图片////summary///paramname=pictureName图片的绝对路径加文件名/parampublicvoidInsertPictures(stringpictureName){Excel.Worksheetworksheet=(Excel.Worksheet)myExcel.ActiveSheet;//后面的数字表示位置,位置默认worksheet.Shapes.AddPicture(pictureName,MsoTriState.msoFalse,MsoTriState.msoTrue,10,10,150,150);}///summary///向Excel中插入图片////summary///paramname=pictureName图片的绝对路径加文件名/param///paramname=left左边距/param///paramname=top右边距/param///paramname=width宽/param///paramname=heigth高/parampublicvoidInsertPictures(stringpictureName,intleft,inttop,intwidth,intheigth){Excel.Worksheetworksheet=(Excel.Worksheet)myExcel.ActiveSheet;worksheet.Shapes.AddPicture(pictureName,MsoTriState.msoFalse,MsoTriState.msoTrue,top,left,heigth,width);}///summary///重命名工作表////summary///paramname=sheetNum工作表序号,从左到右,从1开始/param///paramname=newSheetName新的工作表名/parampublicvoidReNameSheet(intsheetNum,stringnewSheetName){Excel.Worksheetworksheet=(Excel.Worksheet)myExcel.Worksheets[sheetNum];worksheet.Name=newSheetName;}///summary///重命名工作表////summary///paramname=oldSheetName原有工作表名/param///paramname=newSheetName新的工作表名/parampublicvoidReNameSheet(stringoldSheetName,stringnewSheetName){Excel.Worksheetworksheet=(Excel.Worksheet)myExcel.Worksheets[oldSheetName];worksheet.Name=newSheetName;}///summary///新建工作表////summary///paramname=sheetName工作表名/parampublicvoidCreateWorkSheet(stringsheetName){Excel.WorksheetnewWorksheet=(Excel.Worksheet)myWorkBook.Worksheets.Add(Type.Missing,Type.Missing,Type.Missing,Type.Missing);newWorksheet.Name=sheetName;}///summary///激活工作表////summary///paramname=sheetName工作表名/parampublicvoidActivateSheet(stringsheetName){Excel.Worksheetworksheet=(Excel.Worksheet)myExcel.Worksheets[sheetName];worksheet.Activate();}///summary///激活工作表////summary///paramname=sheetNum工作表序号/parampublicvoidActivateSheet(intsheetNum){Excel.Worksheetworksheet=(Excel.Worksheet)myExcel.Worksheets[sheetNum];worksheet.Activate();}///summary///删除一个工作表////summary///paramname=SheetName删除的工作表名/parampublicvoidDeleteSheet(intsheetNum){((Excel.Worksheet)myWorkBook.Worksheets[sheetNum]).Delete();}///summary///删除一个工作表////summary///paramname=SheetName删除的工作表序号/parampublicvoidDeleteSheet(stringsheetName){((Excel.Worksheet)myWorkBook.Worksheets[sheetName]).Delete();}///summary///合并单元格////summary///paramname=startRow起始行/param///paramname=startColumn起始列/param///paramname=endRow结束行/param///paramname=endColumn结束列/parampublicvoidCellsUnite(intstartRow,intstartColumn,intendRow,intendColumn){Excel.Rangerange=myExcel.get_Range(myExcel.Cells[startRow,startColumn],myExcel.Cells[endRow,endColumn]);range.MergeCells=true;}///summary///单元格文字对齐方式////summary///paramname=startRow起始行/param///paramname=startColumn起始列/param///paramname=endRow结束行/param///paramname=endColumn结束列/param///paramname=hAlign水平对齐/param///paramname=vAlign垂直对齐/parampublicvoidCellsAlignment(intstartRow,intstartColumn
本文标题:C#操作Excel的类
链接地址:https://www.777doc.com/doc-5015668 .html