ds导入EXCEL

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;

/// <summary>
///PriceExcel 的摘要说明
/// </summary>
public class PriceExcel
{
    public PriceExcel()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
    }

    public static void ExportToExcel(DataSet ds, string fileStrName)
    {

        Excel.Application excel = new Excel.ApplicationClass();

        if (excel == null)
        {

            MessageBox.Show("Excel无法启动", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);

        }

        int rowindex = 2;

        int colindex = 0;

        excel.Application.Workbooks.Add(true);

        DataTable dt = ds.Tables[0];

        Excel.Range range = excel.get_Range(excel.Cells[1, 1], excel.Cells[1, dt.Columns.Count]);

        range.MergeCells = true;
        range.Cells.Interior.Color = System.Drawing.Color.FromArgb(255, 204, 153).ToArgb();//单元格背景

        range.WrapText = true;
       
        excel.ActiveCell.FormulaR1C1 = fileStrName;

        excel.ActiveCell.Font.Size = 18;

        excel.ActiveCell.Font.Bold = true;

        foreach (DataColumn col in dt.Columns)
        {

            colindex = colindex + 1;

            excel.Cells[2, colindex] = col.ColumnName;

        }

        foreach (DataRow row in dt.Rows)
        {

            colindex = 0;

            rowindex++;

            foreach (DataColumn col in dt.Columns)
            {

                colindex++;

                excel.Cells[rowindex, colindex] = row[col.ColumnName].ToString();

            }

        }
        //输入公式
        //rowindex++;
        //excel.Cells[rowindex, 1] = "=SUM(A3+A4)";
        //excel.Cells[rowindex, 2] = "=AVERAGE(B3:B5)";

        excel.get_Range(excel.Cells[1, 1], excel.Cells[1, dt.Columns.Count]).Font.Bold = true;

        excel.get_Range(excel.Cells[1, 1], excel.Cells[rowindex, colindex]).Borders.LineStyle = 0;

        excel.Cells.EntireColumn.AutoFit();

        excel.Cells.VerticalAlignment = Excel.Constants.xlCenter;

        excel.Cells.HorizontalAlignment = Excel.Constants.xlCenter;

        try
        {

            excel.Visible = false;

            excel.Save("sheet1");

 

        }

        catch { }

        finally
        {

            excel.Quit();

            excel = null;

        }

    }

    public static void ExcelPriceByID(List<int> ticketOrderID)
    {
        TBS.BLL.OrderBLL.OrderBLL orderBLL = new TBS.BLL.OrderBLL.OrderBLL();
        DataSet ds = new DataSet();
        DataSet ds2 = new DataSet();
        foreach (int i in ticketOrderID)
        {
            ds2 = orderBLL.GetDataSet(new TBS.Model.TicketOrder(), null, " [TicketOrderID]=" + i, -1, -1);
            ds.Merge(ds2);
        }
        ds.Tables[0].Columns.Remove("AllPrice");
        ds.Tables[0].Columns.Remove("Sms");
        ExportToExcel(ds, "TicketOrder");
    }
}

 

第二种方法:

 

 private void ExportExcel(string where)
    {
        TBS.BLL.PiaoPointBLL.ReplyBLL replyBLL = new TBS.BLL.PiaoPointBLL.ReplyBLL();
        DataSet ds = replyBLL.GetDataset("[Reply].*", where, -1, -1);
        StringWriter sw = new StringWriter();
        sw.WriteLine("留言消息\t评论用户\t评论时间");

        for (int i = 0; i < ds.Tables[0].Rows.Count;i++ )
        {
            string strs = ds.Tables[0].Rows[i]["message"].ToString() + "\t" + ds.Tables[0].Rows[i]["UserName"].ToString() + "\t" + ds.Tables[0].Rows[i]["InsertTime"].ToString();
            sw.WriteLine(strs);
        }
        sw.Close();
        Response.AddHeader("Content-Disposition", "attachment; filename=Message.xls");
        Response.ContentType = "application/ms-excel";
        Response.ContentEncoding = Encoding.GetEncoding("GB2312");
        Response.Write(sw);
        Response.End();
        ds = null;
    }

 

//excel属性

http://shandengx.blog.163.com/blog/static/3595826920084312549851/

你可能感兴趣的:(Excel)