郑贤

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::


从Excel中导出数据:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; 
using System.Data;
using System.Text;
using System.Net; 
using System.Data.OleDb;
using DAL;

  public class address
        {

            public string district { get; set; }
            public string street { get; set; }
            public string police { get; set; }
        }

    protected void Button1_Click(object sender, EventArgs e)
        {

  string file = @"C:\xingyi.xls"; 
            List<address> list = new List<address>();
            string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + file + ";" + "Extended Properties=Excel 8.0;";
            OleDbConnection conn = new OleDbConnection(strConn);
            conn.Open();

            string strExcel = "";
            OleDbDataAdapter myCommand = null;
            DataSet ds = new DataSet();
            strExcel = "select * from [sheet1$]";
            myCommand = new OleDbDataAdapter(strExcel, strConn);

            myCommand.Fill(ds, "table1");

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                list.Add(
                    new address
                    {
                        street = ds.Tables[0].Rows[i]["道路"].ToString(),
                        police = ds.Tables[0].Rows[i]["所在辖区"].ToString()
                    }
                    );
            }
      }
View Code

 


从Excel中导入数据:

 

using DPC.Model;
using DPC.Web.Common;
using DPC.Web.WebBase;
using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;
using System.Web;
using System.Web.UI;


  protected void exportExcel_Click(object sender, EventArgs e)
        {
               DataSet ds = bllRegist.GetDataCountbyType("akskfhdskhfksnfkwehfisdhfndskfhsid");
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataTable dt = ds.Tables[0];

                // Open the Excel file
                FileInfo file = new FileInfo(@Server.MapPath("\\Model\\类型统计.xlsx"));

                #region Excel export

              
                using (ExcelPackage package = new ExcelPackage(file))
                {
                    
                    ExcelWorksheet sheet = package.Workbook.Worksheets[1];

                    int colCount = dt.Columns.Count;
                  
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        int numb = i + 2;
                        //
                        sheet.Cells["A" + numb].Value = dt.Rows[i]["province"].ToString();
                        //
                        sheet.Cells["B" + numb].Value = dt.Rows[i]["city"].ToString();
                        // 区县
                        sheet.Cells["C" + numb].Value = dt.Rows[i]["district"].ToString();
                        // 发卡点
                        sheet.Cells["D" + numb].Value = dt.Rows[i]["RESERVE2"].ToString();
                        // 名称
                        sheet.Cells["E" + numb].Value = typename;
                        // 数量
                        sheet.Cells["F" + numb].Value =Convert.ToInt32( dt.Rows[i]["num"].ToString());
                    }

                    string tickName = DateTime.Now.ToString("yyyyMMddhhmmss") + "_类型统计.xlsx";

                    MemoryStream ms = new MemoryStream();
                    package.SaveAs(ms);

                    //asp.net输出的Excel文件名  
                    //如果文件名是中文的话,需要进行编码转换,否则浏览器看到的下载文件是乱码。  
                    string fileName = HttpUtility.UrlEncode(tickName);

                    Response.ContentType = "application/vnd.ms-excel";
                    //Response.ContentType = "application/download"; //也可以设置成download  
                    Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", fileName));

                    Response.Buffer = true;
                    Response.Clear();
                    Response.BinaryWrite(ms.GetBuffer());
                    Response.End();
                }
                #endregion
                

            }


        }
View Code

 

posted on 2018-05-31 11:54  郑志岩  阅读(159)  评论(0编辑  收藏  举报