[C#] C#操作Excell常用方法 →→→→→进入此内容的聊天室

来自 , 2021-03-19, 写在 C#, 查看 132 次.
URL http://www.code666.cn/view/fb89fd13
  1. range.NumberFormatLocal = "@";     //设置单元格格式为文本
  2.  
  3.  
  4. range = (Range)worksheet.get_Range("A1", "E1");     //获取Excel多个单元格区域:本例做为Excel表头
  5.  
  6. range.Merge(0);     //单元格合并动作
  7.  
  8. worksheet.Cells[1, 1] = "Excel单元格赋值";     //Excel单元格赋值
  9.  
  10. range.Font.Size = 15;     //设置字体大小
  11.  
  12. range.Font.Underline=true;     //设置字体是否有下划线
  13.  
  14. range.Font.Name="黑体";      设置字体的种类
  15.  
  16. range.HorizontalAlignment=XlHAlign.xlHAlignCenter;     //设置字体在单元格内的对其方式
  17.  
  18. range.ColumnWidth=15;     //设置单元格的宽度
  19.  
  20. range.Cells.Interior.Color=System.Drawing.Color.FromArgb(255,204,153).ToArgb();     //设置单元格的背景色
  21.  
  22. range.Borders.LineStyle=1;     //设置单元格边框的粗细
  23.  
  24. range.BorderAround(XlLineStyle.xlContinuous,XlBorderWeight.xlThick,XlColorIndex.xlColorIndexAutomatic,System.Drawing.Color.Black.ToArgb());     //给单元格加边框
  25.  
  26. range.Borders.get_Item(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeTop).LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlLineStyleNone; //设置单元格上边框为无边框
  27.  
  28. range.EntireColumn.AutoFit();     //自动调整列宽
  29.  
  30. Range.HorizontalAlignment= xlCenter;     // 文本水平居中方式
  31.  
  32. Range.VerticalAlignment= xlCenter     //文本垂直居中方式
  33.  
  34. Range.WrapText=true;     //文本自动换行
  35.  
  36. Range.Interior.ColorIndex=39;     //填充颜色为淡紫色
  37.  
  38. Range.Font.Color=clBlue;     //字体颜色
  39.  
  40. xlsApp.DisplayAlerts=false;     //保存Excel的时候,不弹出是否保存的窗口直接进行保存
  41.  
  42. ====================================================================
  43.  
  44. using System;
  45. using System.Collections.Generic;
  46. using System.Text;
  47. using System.Reflection;
  48. using System.Runtime.InteropServices;
  49. using Microsoft.Office.Interop.Excel;
  50. using ExcelApplication = Microsoft.Office.Interop.Excel.ApplicationClass;
  51. using System.IO;
  52. namespace ExcalDemo
  53. {
  54.     public class ExcelFiles
  55.     {
  56.         public void CreateExcelFiles()
  57.         {
  58.             ExcelApplication excel = new ExcelApplication();
  59.             try
  60.             {
  61.                 excel.Visible = false;// 不显示 Excel 文件,如果为 true 则显示 Excel 文件
  62.                 excel.Workbooks.Add(Missing.Value);// 添加工作簿
  63.                 Worksheet sheet = (Worksheet)excel.ActiveSheet;// 获取当前工作表
  64.                 Range range = null;// 创建一个空的单元格对象
  65.                
  66.                 range = sheet.get_Range("A1", Missing.Value);// 获取单个单元格
  67.                 range.RowHeight = 20;           // 设置行高
  68.                 range.ColumnWidth = 20;         // 设置列宽
  69.                 range.Borders.LineStyle = 1;    // 设置单元格边框
  70.                 range.Font.Bold = true;         // 加粗字体
  71.                 range.Font.Size = 20;           // 设置字体大小
  72.                 range.Font.ColorIndex = 5;      // 设置字体颜色
  73.                 range.Interior.ColorIndex = 6; // 设置单元格背景色
  74.                 range.HorizontalAlignment = XlHAlign.xlHAlignCenter;// 设置单元格水平居中
  75.                 range.VerticalAlignment = XlVAlign.xlVAlignCenter;// 设置单元格垂直居中
  76.                 range.Value2 = "设置行高和列宽";// 设置单元格的值
  77.                 range = sheet.get_Range("B2", "D4");// 获取多个单元格
  78.                 range.Merge(Missing.Value);         // 合并单元格
  79.                 range.Columns.AutoFit();            // 设置列宽为自动适应
  80.                 range.NumberFormatLocal = "#,##0.00";// 设置单元格格式为货币格式
  81.         // 设置单元格左边框加粗
  82.                 range.Borders[XlBordersIndex.xlEdgeLeft].Weight = XlBorderWeight.xlThick;
  83.         // 设置单元格右边框加粗
  84.                 range.Borders[XlBordersIndex.xlEdgeRight].Weight = XlBorderWeight.xlThick;
  85.                 range.Value2 = "合并单元格";
  86.                 // 页面设置
  87.                 sheet.PageSetup.PaperSize = XlPaperSize.xlPaperA4;          // 设置页面大小为A4
  88.                 sheet.PageSetup.Orientation = XlPageOrientation.xlPortrait; // 设置垂直版面
  89.                 sheet.PageSetup.HeaderMargin = 0.0;                         // 设置页眉边距
  90.                 sheet.PageSetup.FooterMargin = 0.0;                         // 设置页脚边距
  91.                 sheet.PageSetup.LeftMargin = excel.InchesToPoints(0.354330708661417); // 设置左边距
  92.                 sheet.PageSetup.RightMargin = excel.InchesToPoints(0.354330708661417);// 设置右边距
  93.                 sheet.PageSetup.TopMargin = excel.InchesToPoints(0.393700787401575); // 设置上边距
  94.                 sheet.PageSetup.BottomMargin = excel.InchesToPoints(0.393700787401575);// 设置下边距
  95.                 sheet.PageSetup.CenterHorizontally = true;                  // 设置水平居中
  96.                 // 打印文件
  97.                 sheet.PrintOut(Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
  98.                
  99.                 // 保存文件到程序运行目录下
  100.                 sheet.SaveAs(Path.Combine(System.Windows.Forms.Application.StartupPath,"demo.xls"), Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
  101.                 excel.ActiveWorkbook.Close(false, null, null); // 关闭 Excel 文件且不保存
  102.             }
  103.             catch (Exception ex)
  104.             {
  105.                 MessageBox.Show(ex.Message);
  106.             }
  107.             finally
  108.             {
  109.                 excel.Quit(); // 退出 Excel
  110.                 excel = null; // 将 Excel 实例设置为空
  111.             }
  112.         }
  113.     }
  114. }
  115.  

回复 "C#操作Excell常用方法"

这儿你可以回复上面这条便签

captcha