日期:2014-05-18  浏览次数:20870 次

如何在导出Excel时控制其单元格的颜色及样式?
如题,前提是不使用office组件,我是用StreamWriter来拼写导出时excel的内容的,请问如何控制excel单元格的样式?
C# code

     FileStream fs = new FileStream(name, FileMode.Create, FileAccess.Write);
                    StreamWriter sw = new StreamWriter(fs, Encoding.GetEncoding("GB2312"));
                    DataTable dt = ds.Tables["table1"];
                    DataTable dtname = GetDataSetName(path).Tables["table1"];
                    string strhead = "";
                    string tempStr = "";
                    string nummonth = "";
                    int count = dtname.Rows.Count;
                    sw.WriteLine("迟到"+"<color='red'>" + "\t" + year + "年" + month + "考勤统计表");
                    sw.WriteLine("早退" + "\t");
                    sw.WriteLine("两者都有" + "\t" + "没有记录");
                    day = GetDay(month, year);
                    for (int i = 1; i <= Convert.ToInt32(day); i++)
                    {
                        if (i > 1)
                        {
                            nummonth += "\t";
                        }
                        nummonth += i.ToString();
                    }
                    sw.WriteLine("姓名" + "\t" + nummonth);

                    for (int i = 0; i < count; i++)
                    {
                        string nameo = dtname.Rows[i][0].ToString() + "\t";
                        string temp = "";
                        for (int j = 1; j <= Convert.ToInt32(day); j++)
                        {
                            temp += GetTimeOffValue(path, dtname.Rows[i][0].ToString(), j.ToString()).ToString() + "\t";
                        }
                        sw.WriteLine(nameo + temp);
                    }
                    sw.Close();



------解决方案--------------------
我当初是做了个excel的模板,写好样式什么的,然后往里面写东西!效果还不错!楼主试试!
------解决方案--------------------
你这导出的不是 Excel,就是一段文本。

Excel 对象模型概述

如何:将颜色应用于 Excel 范围(Range)
------解决方案--------------------
那导出成 html 格式 然后 excel 打开