码迷,mamicode.com
首页 > Windows程序 > 详细

使用C#数据如何导到excel

时间:2016-01-10 18:40:25      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:

能够通过相关代码生成一个excel表格,并能对表格样式等进行控制~

public void ExportExcel()
        {
            //创建一个 Excel 实例
            ExcelApplication excel = new ExcelApplication();
            try
            {               
                excel.Visible = false;// 不显示 Excel 文件,如果为 true 则显示 Excel 文件
                excel.Workbooks.Add(Missing.Value);// 添加工作簿
                Worksheet sheet = (Worksheet)excel.ActiveSheet;// 获取当前工作表
                Range range = null;// 创建一个空的单元格对象
                sheet.get_Range(sheet.Cells[29, 2], sheet.Cells[29, 2]).Orientation = Microsoft.Office.Interop.Excel.XlOrientation.xlVertical;//设置29行第2个单元格的字体竖直居中在单元格内
                range = sheet.get_Range("A1", Missing.Value);// 获取单个单元格
                range.RowHeight = 20;           // 设置行高
                range.ColumnWidth = 20;         // 设置列宽
                range.Borders.LineStyle = 1;    // 设置单元格边框
                range.Font.Bold = true;         // 加粗字体
                range.Font.Size = 20;           // 设置字体大小              
  range.Font.ColorIndex =5;// 设置字体颜色 1:黑色;2:白色;3:红色;4:绿色;5:蓝色          
                range.Interior.ColorIndex = 6;  // 设置单元格背景色
 
 
                range.Interior.ColorIndex = 6;  // 设置单元格背景色
                range.HorizontalAlignment = XlHAlign.xlHAlignCenter;// 设置单元格水平居中
                range.VerticalAlignment = XlVAlign.xlVAlignCenter;// 设置单元格垂直居中
                range.Value2 = "设置行高和列宽";// 设置单元格的值
 
 
                range = sheet.get_Range("B2", "D4");// 获取多个单元格
                range.Merge(Missing.Value);         // 合并单元格
                range.Columns.AutoFit();            // 设置列宽为自动适应
                range.NumberFormatLocal = "#,##0.00";// 设置单元格格式为货币格式
                // 设置单元格左边框加粗
                range.Borders[XlBordersIndex.xlEdgeLeft].Weight = XlBorderWeight.xlThick;
                // 设置单元格右边框加粗
                range.Borders[XlBordersIndex.xlEdgeRight].Weight = XlBorderWeight.xlThick;
                range.Value2 = "合并单元格";
range = sheet.get_Range("A1", "C15");// 获取多个单元格
                range.Cells.Borders.LineStyle = 1;//设置全边框
 
                // 页面设置
                sheet.PageSetup.PaperSize = XlPaperSize.xlPaperA4;          // 设置页面大小为A4
                sheet.PageSetup.Orientation = XlPageOrientation.xlPortrait; // 设置垂直版面
                sheet.PageSetup.HeaderMargin = 0.0;                         // 设置页眉边距
                sheet.PageSetup.FooterMargin = 0.0;                         // 设置页脚边距
                sheet.PageSetup.LeftMargin = excel.InchesToPoints(0.354330708661417); // 设置左边距
                sheet.PageSetup.RightMargin = excel.InchesToPoints(0.354330708661417);// 设置右边距
                sheet.PageSetup.TopMargin = excel.InchesToPoints(0.393700787401575);  // 设置上边距
                sheet.PageSetup.BottomMargin = excel.InchesToPoints(0.393700787401575);// 设置下边距
                sheet.PageSetup.CenterHorizontally = true;                  // 设置水平居中
 
                // 如需打印文件
             //   sheet.PrintOut(Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
 
                // 保存文件到程序运行目录下
                
                sheet.SaveAs("D:\\hope.xls", Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
                excel.ActiveWorkbook.Close(false, null, null); // 关闭 Excel 文件且不保存
                MessageBox.Show("生成成功!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

使用C#数据如何导到excel

标签:

原文地址:http://www.cnblogs.com/cat-123-ofmine/p/5118831.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!