RT,各位大虾们最好能贴上代码,方便理解!

解决方案 »

  1.   

     google一下吧,很多vc操作excel教程的.
      

  2.   

    http://www.vckbase.com/document/viewdoc/?id=1168
      

  3.   


    // OFFICE-EXCEL 从DataTable导出EXCEL文件// winform 从DataTable导出EXCEL文件
    public void ExportExcelOffice(DataTable table, string filepath)
    {    Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
        excel.Visible = false;
        object ms = Type.Missing;
        Excel.Workbook wk = excel.Workbooks.Add(ms);
        Excel.Worksheet ws = wk.Worksheets[1] as Excel.Worksheet;
        for (int i = 0; i < table.Columns.Count; i++)
        {
            ws.Cells[1, i + 1] = table.Columns[i].ColumnName;
        }
        for (int i = 0; i < table.Rows.Count; i++)
        {
            for (int j = 0; j < table.Columns.Count; j++)
            {
                ws.Cells[i + 2, j + 1] = table.Rows[i][j].ToString();
            }
        }    if (File.Exists(filepath) == false)
        {
            Directory.CreateDirectory(filepath);
        }
        wk.SaveAs(filepath, ms, ms, ms, ms, ms, Excel.XlSaveAsAccessMode.xlShared, ms, ms, ms, ms, ms);
        excel.Quit();}// webform 从DataTable导出EXCEL文件
    public void ExportExcelStream(DataTable table, string filepath)
    {
        StringWriter stringWriter = new StringWriter();
        HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
        DataGrid excel = new DataGrid();
        System.Web.UI.WebControls.TableItemStyle AlternatingStyle = new TableItemStyle();
        System.Web.UI.WebControls.TableItemStyle headerStyle = new TableItemStyle();
        System.Web.UI.WebControls.TableItemStyle itemStyle = new TableItemStyle();
        AlternatingStyle.BackColor = System.Drawing.Color.LightGray;
        headerStyle.BackColor = System.Drawing.Color.LightGray;
        headerStyle.Font.Bold = true;
        headerStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
        itemStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center; ;    excel.AlternatingItemStyle.MergeWith(AlternatingStyle);
        excel.HeaderStyle.MergeWith(headerStyle);
        excel.ItemStyle.MergeWith(itemStyle);
        excel.GridLines = GridLines.Both;
        excel.HeaderStyle.Font.Bold = true;
        excel.DataSource = table.DefaultView;  //输出DataTable的内容 
        excel.DataBind();
        excel.RenderControl(htmlWriter);    string filestr = filepath;
        int pos = filestr.LastIndexOf("\\");
        string file = filestr.Substring(0, pos);
        if (!Directory.Exists(file))
        {
            Directory.CreateDirectory(file);
        }
        System.IO.StreamWriter sw = new StreamWriter(filestr);
        sw.Write(stringWriter.ToString());
        sw.Close();
    }