C#操作excel行颜色问题

moonzap 2011-10-18 01:17:40
现在C#中要将某颜色比如greenyellow设置到excel行背景上.
设置后,颜色有偏差,如何解决.
...全文
427 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
moonzap 2011-10-18
  • 打赏
  • 举报
回复
有高手知道吗?
moonzap 2011-10-18
  • 打赏
  • 举报
回复
如何解决这样的问题?有什么好方法吗?各位能人
moonzap 2011-10-18
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 keenweiwei 的回复:]
设置后有偏差?

指什么?
[/Quote]
比如excel的颜色是RGB(173,255,47)的颜色,通过C#
....Interior.Color = ColorTranslator.ToOle(Color.GreenYellow);
设置后,得到的颜色,是有偏差的.
yaodan11333 2011-10-18
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 moonzap 的回复:]
引用 3 楼 yaodan11333 的回复:
那就换个颜色,你百度下,C#颜色和名称样式对照表,里面有各种颜色对应的名称的


问题不是这个,而是设置后有偏差.
[/Quote]

这个你可以看看,里面各种颜色:http://www.cnblogs.com/ouyanga/archive/2011/05/26/2058096.html
你所谓的偏差,只的是不同的显示器显示出的色差吗?
ijwsoft 2011-10-18
  • 打赏
  • 举报
回复
设置后有偏差?

指什么?
moonzap 2011-10-18
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 yaodan11333 的回复:]
那就换个颜色,你百度下,C#颜色和名称样式对照表,里面有各种颜色对应的名称的
[/Quote]

问题不是这个,而是设置后有偏差.
yaodan11333 2011-10-18
  • 打赏
  • 举报
回复
那就换个颜色,你百度下,C#颜色和名称样式对照表,里面有各种颜色对应的名称的
moonzap 2011-10-18
  • 打赏
  • 举报
回复
这个是要求,我也想那么做...
cjh200102 2011-10-18
  • 打赏
  • 举报
回复
你取个标准的色不就行了。
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 实例设置为空 } } } }

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧