我用 C# (.net 2003) 操作 excel ,修改 excel 单元格背景色,问题是C#使用ColorIndex修改的值和 VBA 中不一样, 比如 ,在 excel 中使用宏得知土黄色是 44,但在 C# 中做 ColorIndex = 44 操作;得到的结果却是黄色。我还用如下代码输出了所以的56种颜色,但没有发现土黄色,还有其它的一些颜色也没有.不知是怎么回事。我分不多,只有21分了。请各位高手帮帮忙
private bool convertExcelFile(string filepath)
{
System.Reflection.Missing mis = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Excel.Application xlApp = null;
Microsoft.Office.Interop.Excel.Workbook xlBook;
Microsoft.Office.Interop.Excel.Worksheet ws;
try
{
xlApp = new Microsoft.Office.Interop.Excel.Application();
xlApp.DisplayAlerts = false;
xlBook = xlApp.Workbooks.Open(filepath, 0, false, mis, mis,mis,mis,mis,mis,mis,mis,mis,mis,mis,mis);
ws = (Microsoft.Office.Interop.Excel.Worksheet)xlBook.Worksheets[1];
int rowcount = ws.UsedRange.Rows.Count;
int colcount = ws.UsedRange.Columns.Count;
string errorMess = "";
for(int i=1;i<=250;i++)
{
try
{
Range r = (Range)ws.Cells[i,1];
r.Interior.ColorIndex = i;
}
catch(Exception ex)
{
errorMess += ex.Message;
}
}
ws.SaveAs(filepath,Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel4Workbook,Type.Missing,Type.Missing,false,false,Type.Missing,Type.Missing,Type.Missing,Type.Missing);
xlBook.Save();
xlApp.Quit();
xlBook = null;
ws = null;
GC.Collect();
}
catch(Exception ex)
{
if(xlApp != null) xlApp.Quit();
xlApp.Quit();
xlBook = null;
ws = null;
GC.Collect();
MessageBox.Show(ex.Message);
return false;
}
return true;
}
private bool convertExcelFile(string filepath)
{
System.Reflection.Missing mis = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Excel.Application xlApp = null;
Microsoft.Office.Interop.Excel.Workbook xlBook;
Microsoft.Office.Interop.Excel.Worksheet ws;
try
{
xlApp = new Microsoft.Office.Interop.Excel.Application();
xlApp.DisplayAlerts = false;
xlBook = xlApp.Workbooks.Open(filepath, 0, false, mis, mis,mis,mis,mis,mis,mis,mis,mis,mis,mis,mis);
ws = (Microsoft.Office.Interop.Excel.Worksheet)xlBook.Worksheets[1];
int rowcount = ws.UsedRange.Rows.Count;
int colcount = ws.UsedRange.Columns.Count;
string errorMess = "";
for(int i=1;i<=250;i++)
{
try
{
Range r = (Range)ws.Cells[i,1];
r.Interior.ColorIndex = i;
}
catch(Exception ex)
{
errorMess += ex.Message;
}
}
ws.SaveAs(filepath,Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel4Workbook,Type.Missing,Type.Missing,false,false,Type.Missing,Type.Missing,Type.Missing,Type.Missing);
xlBook.Save();
xlApp.Quit();
xlBook = null;
ws = null;
GC.Collect();
}
catch(Exception ex)
{
if(xlApp != null) xlApp.Quit();
xlApp.Quit();
xlBook = null;
ws = null;
GC.Collect();
MessageBox.Show(ex.Message);
return false;
}
return true;
}
解决方案 »
- XML
- c# 在第一个Excel表的最后插入,另一个EXCEL表的所有数据
- 大家讲讲在开发中是如何处理SQL语句的?
- C++ char strClientIP[6][16],C#以托管方式怎么定义呢?
- 调用WEBSERVERICE时发生错误
- 国庆节好,请问论坛晚上怎么上不去?晚上几点关闭论坛?
- Asp.net视频文件中用到视频控件如何加载 !急,真诚期盼您的回复!!!!!!!
- socket接收问题,请高手帮忙,分不够再加!
- 【砝码均衡问题】算法大牛支持,各种大神请进
- 关于继承的方法在VS.NET中显示的问题。
- LPSAFEARRAY 在C#中的转化
- 正则表达式,数字不能大于12位字符【速度给分】
可以试试这个
Color color=Color.FromName(ColorName);
color=Color.FromArgb(color.B,color.G,color.R);
ExcelWorkSheet.get_Range(ExcelWorkSheet.Cells[SelectedStartRow,SelectedStartCol],ExcelWorkSheet.Cells[SelectedEndRow,SelectedEndCol]).Font.Color=color.ToArgb();本文来自: 站长(http://www.qqcf.com) 详细出处参考:http://study.qqcf.com/web/714/197760.htm
my blog
http://ufo-crackerx.blog.163.com/