博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# 操作Excel
阅读量:7284 次
发布时间:2019-06-30

本文共 4116 字,大约阅读时间需要 13 分钟。

从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
list = new List
(); 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

 

转载于:https://www.cnblogs.com/hanxiaofei/p/9115911.html

你可能感兴趣的文章
ECMAScript正则表达式6个最新特性
查看>>
android Studio 快捷键
查看>>
MySQL Explain
查看>>
Java NIO
查看>>
1、图片水印 之 一
查看>>
分布式锁
查看>>
使用proxychains-ng代理转发终端命令
查看>>
mysql初始化错误
查看>>
shell中的函数,shell中的数组,告警系统需求分析
查看>>
df命令 、du命令 、磁盘分区
查看>>
Java并发编程:CountDownLatch、CyclicBarrier和 Semaphore
查看>>
使用JDK自带的jmap和jhat监控处于运行状态的Java进程
查看>>
App怎么免费接入聚合支付SDK?
查看>>
什么是TensorFlow?
查看>>
Web3与智能合约交互实战(附代码)
查看>>
选择日期:年月日时分秒 插件
查看>>
从零到有的突破:BCH爱好者聚集地BCH.Club公测上线
查看>>
自建容器映像登录也行?Quay整合多种进阶功能
查看>>
如何快速打造一款高清又极速的短视频APP?
查看>>
总结sqlserver2012删除语法:delete数据表中的某一项内容
查看>>