C#操作Excel 单元格的格式处理[xyytIT]

一. C# 操作 Excel 单元格自动填充,居中对齐,字体颜色等格式设置:

 1 Excel.Range titleRange = worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[1, columnCount]);//选取单元格,选取一行或多行
 2 titleRange.Merge(true);//合并单元格
 3 titleRange.Value2 = strTitle; //设置单元格内文本
 4 titleRange.Value2 = Type.Missing;//清除单元格内容
 5 titleRange.Font.Name = "宋体";//设置字体
 6 titleRange.Font.Size = 18;//字体大小
 7 titleRange.Font.Bold = true;//加粗显示
 8 titleRange.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;//水平居中
 9 titleRange.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;//垂直居中
10 titleRange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;//设置边框样式
11 titleRange.Borders.Weight = Excel.XlBorderWeight.xlMedium;//设置边框宽度/* 设置每个单元格字体居中 */ 
12 excel.Columns.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;//设置每个单元格字体居中
13 excel.Columns.ColumnWidth = 14;//设置每个单元格的宽度
14 excel.get_Range(sheet.Cells[2, 1], sheet.Cells[rows+1, 1]).NumberFormat = "yyyy/mm/dd"; //时间格式的设置
15 range.NumberFormat = "@";//设置为数值格式

 二. C#生成Excel合并单元格的操作:

1. 合并单元格的操作:

1 Excel.Range range= worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[1, columnCount]);//选取单元格,选取一行或多行
2 range.Value2 = Type.Missing; //必须的!由于excel本身的设计中考虑了与用户的交互,当此range中有值时,会弹出对话框。所以为了防止这种事情发生,应当清空值
3 rangesummary1.Merge(Type.Missing);

2. 给合并的单元格赋值:

正常的情况下,不需要考虑单元格合并的,直接使用循环对所有的单元格进行赋值即可。

对于下面的订单表,每个订单可能会有多件商品信息,就需要单元格合并,怎么赋值呢?

问题解析:对比上面两个表,除了第二个表中订单号,支付方式都进行了合并,其余内容都是一样的。

对我们来说,实现第一个表的赋值是没什么问题的,但是如何实现第二个表格呢,这里涉及两个问题:

1.合并单元格

2.对合并后的单元格进行赋值

单元格合并后,表格没有了原来的标准格局,如何进行循环赋值呢?

解决方法:先进行单元格合并,然后按实现第一个表的方法对每一个单元格进行赋值。

其实这个问题并没有想象中的那么复杂,只要能实现第一个表格的赋值和合并单元格的操作就好。

无需考虑如何对合并的单元格赋值,当我们循环对每一行进行复制的时候,每个合并的单元格会在当前行赋值的时候进行赋值,如果第二行中还包含这个合并单元格(当前行已经没有此单元格),就自动忽略,不会影响后面的单元格赋值。

 

转载于:https://www.cnblogs.com/xyyt/p/3480256.html

range.NumberFormatLocal = "@"; //设置单元格格式为文本 range = (Range)worksheet.get_Range("A1", "E1"); //获取Excel多个单元格区域:本例做为Excel表头 range.Merge(0); //单元格合并动作 worksheet.Cells[1, 1] = "Excel单元格赋值"; //Excel单元格赋值 range.Font.Size = 15; //设置字体大小 range.Font.Underline=true; //设置字体是否有下划线 range.Font.Name="黑体"; 设置字体的种类 range.HorizontalAlignment=XlHAlign.xlHAlignCenter; //设置字体在单元格内的对其方式 range.ColumnWidth=15; //设置单元格的宽度 range.Cells.Interior.Color=System.Drawing.Color.FromArgb(255,204,153).ToArgb(); //设置单元格的背景色 range.Borders.LineStyle=1; //设置单元格边框的粗细 range.BorderAround(XlLineStyle.xlContinuous,XlBorderWeight.xlThick,XlColorIndex.xlColorIndexAutomatic,System.Drawing.Color.Black.ToArgb()); //给单元格加边框 range.Borders.get_Item(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeTop).LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlLineStyleNone; //设置单元格上边框为无边框 range.EntireColumn.AutoFit(); //自动调整列宽 Range.HorizontalAlignment= xlCenter; // 文本水平居中方式 Range.VerticalAlignment= xlCenter //文本垂直居中方式 Range.WrapText=true; //文本自动换行 Range.Interior.ColorIndex=39; //填充颜色为淡紫色 Range.Font.Color=clBlue; //字体颜色 xlsApp.DisplayAlerts=false; //保存Excel的时候,不弹出是否保存的窗口直接进行保存 ==================================================================== using System; using System.Collections.Generic; using System.Text; using System.Reflection; using System.Runtime.InteropServices; using Microsoft.Office.Interop.Excel; using ExcelApplication = Microsoft.Office.Interop.Excel.ApplicationClass; using System.IO; namespace ExcalDemo { public class ExcelFiles { public void CreateExcelFiles() { ExcelApplication excel = new ExcelApplication(); try { excel.Visible = false;// 不显示 Excel 文件,如果为 true 则显示 Excel 文件 excel.Workbooks.Add(Missing.Value);// 添加工作簿 Worksheet sheet = (Worksheet)excel.ActiveSheet;// 获取当前工作表 Range range = null;// 创建一个空的单元格对象 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; // 设置字体颜色 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 = "合并单元格"; // 页面设置 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(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); excel.ActiveWorkbook.Close(false, null, null); // 关闭 Excel 文件且不保存 } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { excel.Quit(); // 退出 Excel excel = null; // 将 Excel 实例设置为空 } } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值