您当前的位置:首页 > ASP.NET > 文章详情

c#操作excel的常用代码

2013-03-183837人围观
微软的Excel对象模型包括了128个不同的对象,从矩形,文本框等简单的对象到透视表,图表等复杂的对象.下面我们简单介绍一下其中最重要的四个对象。 (1) Application对象。Application对象处于Excel对象层次结构的顶层,表示Excel自身的运行环境。 (2) Workbook对象。Workbook对象直接地处于Application对象的下层,表示一个Excel工作薄文件。 (3) Worksheet对象。Worksheet对象包含于Workbook对象,表示一个Excel工作表。 (4) Range对象。Range对象包含于Worksheet对象,表示Excel工作表中的一个或多个单元格。 C#操作代码样列: object miss = Missing.Value;//缺省变量 Excel.Application excel = new Excel.Application ();//引用Excel对象 excel.Application.Workbooks.Add ( true );//引用Excel工作簿 excel.Visible = true ;//使Excel可视 excelApp.Caption = "测试报表"; workSheet.Name = "学生信息表"; workSheet.Columns.ColumnWidth = 20;//全局行宽 workSheet.Columns.RowHeight = 20;//全局列高 workSheet.PageSetup.PaperSize = XlPaperSize.xlPaperA4;//纸张大小 workSheet.PageSetup.PrintTitleRows = "$1:$3";//顶端标题行 workSheet.PageSetup.Orientation = XlPageOrientation.xlLandscape;//页面方向为横向 workSheet.PageSetup.TopMargin = excelApp.CentimetersToPoints(2);//上边距为2厘米(厘米转像素) workSheet.PageSetup.BottomMargin = excelApp.CentimetersToPoints(2);//下边距为2厘米(厘米转像素) workSheet.PageSetup.LeftMargin = excelApp.CentimetersToPoints(1.5);//左边距为1.5厘米(厘米转像素) workSheet.PageSetup.RightMargin = excelApp.CentimetersToPoints(1.5);//右边距为1.5厘米(厘米转像素) workSheet.PageSetup.HeaderMargin = excelApp.CentimetersToPoints(1.2);//页眉边距为1.2厘米(厘米转像素) workSheet.PageSetup.FooterMargin = excelApp.CentimetersToPoints(1);//页脚边距为1厘米(厘米转像素) workSheet.PageSetup.CenterHorizontally = true;//页面水平居中 workSheet.PageSetup.CenterVertically = false;//页面不垂直居中 workSheet.PageSetup.CenterFooter = "第&P页,共&N页";//中间页脚内容 range1.HorizontalAlignment = XlHAlign.xlHAlignCenter;//设置水平对齐方式 range1.VerticalAlignment = XlVAlign.xlVAlignCenter;//设置垂直对齐方式 workSheet.get_Range(workSheet.Cells[1, colIndex], workSheet.Cells[1, colIndex])//选择操作块 Range range1 = workSheet.get_Range("A1", "E20");//选择操作块 range1.Borders.Color = System.Drawing.ColorTranslator.ToOle(Color.Red);//设置边框颜色 range1.Borders.get_Item(XlBordersIndex.xlDiagonalDown).LineStyle = XlLineStyle.xlContinuous;//斜杠 range1.Borders.get_Item(XlBordersIndex.xlDiagonalUp).LineStyle = XlLineStyle.xlContinuous;//反斜杠 range1.Borders.get_Item(XlBordersIndex.xlDiagonalDown).Color = System.Drawing.ColorTranslator.ToOle(Color.Gold); range1.Borders.get_Item(XlBordersIndex.xlInsideHorizontal).LineStyle = XlLineStyle.xlContinuous;//块内竖线 range1.Borders.get_Item(XlBordersIndex.xlInsideVertical).LineStyle = XlLineStyle.xlContinuous;//块内横线 //统一设置边框风格 range1.BorderAround(XlLineStyle.xlDouble, XlBorderWeight.xlThick, XlColorIndex.xlColorIndexAutomatic, System.Drawing.Color.Black.ToArgb()); range1.Font.Bold = true;//设置黑体 range1.Font.Size = 18;//设置字体大小 range1.Font.Name = "仿宋";//设置字体 range1.Font.Color = System.Drawing.ColorTranslator.ToOle(Color.Blue);//设置字体颜色 excelApp.Application.DisplayAlerts = false;//使合并操作不提示警告信息 range1.Merge(true);//参数为True则为每一行合并为一个单元格        */ Range range1 = (Range)workSheet.Cells[2, 2];//选择操作块 range1.NumberFormatLocal = "@";     //设置格式为文本 range1.Cells.ColumnWidth = 11; //单元格列高 range1.Rows.ColumnWidth = 15;//单元格行宽 workSheet.Cells[ 3, 1] = "成绩总结"; //写入数据 ((Excel.Range)workSheet.Rows[ 3, Type.Missing]).PageBreak = (int)Excel.XlPageBreak.xlPageBreakManual;//设置分页线 workSheet.PageSetup.PrintArea="A1:O12,C12:B12";//设置打印区域 workBook.RefreshAll();//更新所有工作表 workBook.SaveAs("C:\\Users\\Administrator\\Desktop\\test.xls", miss, miss, miss, miss, miss, XlSaveAsAccessMode.xlNoChange, miss, miss, miss, miss, miss); //保存WorkBook workBook = null;//消除对象 excelApp.Quit(); //这一句是非常重要的,否则Excel对象不能从内存中退出 excelApp = null; GC.Collect();//强制对所有代进行垃圾回收。

文章评论

热门评论
暂无评论