C#將數據輸出為Excel問題
一般用Response.Write輸出一個DateSet的方式網上很多,但都不是我要的,
现遇到的问题是:
1.我的数据字段中含电话号码,所以在匯入Excel后第一个0会被去掉,还有如时间格式是 00:00 二位的在輸入Excel中後會格式化成 00:00:00  
2.內容中含超长文本,文本含HMTL符,有些还有乱码符,HTML符处理应可以,但乱码符在汇入Excel後(以XML方式写入),由于乱码造成Excel打开失败。
不知有没有更好的方法可以解决上述问题,并成功输出Excel文件。
------解决方案--------------------
读出来应该加格式控制:
C# code
     private static void DataSetExportToExcel(cel()
{
------解决方案--------------------
学习!!!!!!!!!!!!!!!!
------解决方案--------------------
[size=12px][/size][size=12px][/size][size=12px][/size][size=12px][/size]
学习
------解决方案--------------------
C# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.IO;
namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            string path = "C:\\ExcelData1.xls";
            if(File.Exists(path))
            {
                File.Delete(path);
            }
            
            string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";localhos
------解决方案--------------------
觉得一楼的用xml转化的方法挺好,虽然看起来比较麻烦,但效率是很高的.并且一楼写的也比较详细,楼主可以考虑下.关注中.
------解决方案--------------------
------解决方案--------------------
这是我程序里的导出EXCEL模块,不知道是不是楼长想要的。
       // 把DataTable内容导出excel并返回客户端  
       public static void DataTable2Excel(System.Data.DataTable dtData)
       {
           System.Web.UI.WebControls.DataGrid dgExport = null;
           // 当前对话  
           System.Web.HttpResponse resp = System.Web.HttpContext.Current.Response;
           // 为了解决dgData中可能进行了分页的情况,需要重新定义一个无分页的DataGrid  
           dgExport = new System.Web.UI.WebControls.DataGrid();
           dgExport.DataSource = dtData.DefaultView;
           dgExport.AllowPaging = false;
           dgExport.DataBind();
           // 返回客户端  
           resp.Clear();
           resp.Buffer = true;
           //page.Response.Write("<meta http-equiv=Content-Type content=text/html>");
           //page.Response.Write("<meta http-equiv=Content-Type content=text/html;charset=gb2312>");
           //resp.ContentEncoding = System.Text.Encoding.GetEncoding("utf-7");//使用GB2312有时候会出现乱码
           resp.ContentEncoding = System.Text.Encoding.UTF7;//使用GB2312有时候会出现乱码
           resp.AppendHeader("content-disposition", "attachment;filename=\"" + HttpUtility.UrlEncode(DateTime.Now.ToString("yyyy-MM-dd"), System.Text.Encoding.UTF8) + ".xls\"");
           //page.EnableViewState = false;
           System.IO.StringWriter tw = new System.IO.StringWriter();
           System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
           dgExport.RenderControl(hw);
           resp.Write(tw.ToString());
           resp.End();
       }
------解决方案--------------------
先顶了再慢慢看。
------解决方案--------------------
------解决方案--------------------