private void OutputProcess()
{
Microsoft.Office.Interop.Excel.Application myExcel = new Microsoft.Office.Interop.Excel.Application();
myExcel.Application.Workbooks.Add(true);myExcel.Cells[1,1]="会员卡余额情况表";
DataTable dt1 = dg1.DataSource as DataTable  ;
int i=0 ;
myExcel.Cells[2,1]="会员编号";
myExcel.Cells[2,2]="名称";
myExcel.Cells[2,3]="会员卡编号";
myExcel.Cells[2,4]="有效期";
myExcel.Cells[2,5]="余额";
myExcel.Cells[2,6]="积分";
foreach(DataRow dr in dt1.Rows )
{
i=i+1;
myExcel.Cells[2+i,1] = dr["fldMemberID"];
myExcel.Cells[2+i,2] = dr["fldName"];
myExcel.Cells[2+i,3] = dr["fldMemberCardNo"];
myExcel.Cells[2+i,4] = dr["fldEndDate"];
myExcel.Cells[2+i,5] = dr["fldAmount"];
myExcel.Cells[2+i,6] = dr["fldIntegral"];}
myExcel.Visible=true;}这是我导出数据成Excel的方法,
导出成功,但是导出后数据格式不对问一下,各位有没有办法设置一下导出时的格式啊,在线等急。。

解决方案 »

  1.   

    这是我自己写的一个类我以前做导出的时候,是直接操作数据源导出的public void ListToXSL(IList<Customer> list, string[] header, string fileName)
        {
            if (list == null || list.Count <= 0)
                return;
            StringBuilder sb = new StringBuilder("<table cellspacing=\"0\" cellpadding=\"0\" rules=\"all\" border=\"1\">");
            if (header.Length > 0)
            {
                sb.AppendFormat("<tr><th rowspan=\"2\">{0}</th><th rowspan=\"2\">{1}</th><th rowspan=\"2\">{2}</th><th colspan=\"2\">{3}</th><th colspan=\"2\">{4}</th><th colspan=\"2\">{5}</th><th colspan=\"2\">{6}</th></tr>",header[0],header[1],header[2],header[3],header[4],header[5],header[6]);
                sb.AppendFormat("<tr><th>数量</th><th>比率</th><th>数量</th><th>比率</th><th>数量</th><th>比率</th><th>数量</th><th>比率</th></tr>");
            }
            foreach (Customer info in list)
            {
                sb.AppendFormat("<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td><td>{6}</td><td>{7}</td><td>{8}</td><td>{9}</td><td>{10}</td></tr>", info.Date, info.City, info.FactAfterServiceCount, info.Oneday, info.OnedayPercent, info.Twoday, info.TwodayPercent, info.Threeday, info.ThreedayPercent, info.More3day, info.More3dayPercent);
            }
            sb.Append("</table>");
            Response.Clear();
            Response.Buffer = true;
            Response.Charset = "GB2312";
            //Response.Charset = "UTF-8";
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName + ".xls", Encoding.UTF8));
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
            Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 
            EnableViewState = false;
            Response.Write(sb.ToString());
            Response.End();
        }你看一下其实没有必要直接去操作EXCEL
    希望能对你有用