您的位置:首页 > 其它

终于完成了自己的Dataset to CSV转换(所见即所得))

2006-04-05 11:08 477 查看
public void ProduceCSV(DataGrid dgListBill,DataSet dsBill)
        {
            int colCount = dgListBill.Columns.Count;
            int valueRowCount = dsBill.Tables[0].Rows.Count;
            string[] dtHeader = new String[colCount];
            
            StringWriter sw=new StringWriter();
            sw.WriteLine("=year(d4)");
            string strColField,strLine;
            strLine ="";
            int k=0;//统计数据绑定的列数;
            //填充表头        
            for (int i=0 ;i<colCount;i++)
            {
                
                string colType = dgListBill.Columns[i].GetType().ToString();
                if (colType == "System.Web.UI.WebControls.BoundColumn")
                {
                    BoundColumn Col = (BoundColumn)dgListBill.Columns[i];
                    if (Col.HeaderText !="操作" && Col.Visible  && Col.DataField !=null)
                    {
                        dtHeader[k] = Col.DataField;
                        strLine += GetWriteableValue(Col.HeaderText)+"    ";
                        k++;
                    }
                }
            }
            sw.WriteLine(strLine);
            //            //填充内容            
            for(int i = 0;i<valueRowCount;i++)
            {
                strLine ="";
                for(int j=0;j<k;j++)
                {
                    strColField = dtHeader[j].ToString();
                    strLine += GetWriteableValue(dsBill.Tables[0].Rows[i][strColField])+"    ";

                }
                sw.WriteLine(strLine);
            }
            ResponseAttach(sw,"输出结果.xls","application/m*-**cel","attachment","gb2312");
            sw.Close();
        }

        public void ResponseAttach(StringWriter fileData,string strFileName,string ContentType,string OpMethod,string EncodingName)
        {
            Response.Clear();
            Response.Charset = "GB2312";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.ContentType = ContentType; 
            Response.AppendHeader("Content-Disposition", OpMethod+";filename=" +Server.UrlEncode(strFileName)); 
            Response.ContentEncoding = System.Text.Encoding.GetEncoding(EncodingName);
            Response.Write(fileData.ToString());
            //Response.Flush();
            Response.End();
            //Response.Clear();
        } 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  dataset csv string null
相关文章推荐