今天我们来看看用Excel下载指定数据库里面的数据。

所用软件:
Microsoft Visual Studio 2010
SQL Server Management Studio

首先,我们要建立一个数据库,本例以Table_4来讲解。
Table_4:

建立好了以后,在VS里面建立三层。不会的参考以前讲过的:https://my.oschina.net/u/3913001/blog/1858562

结构代码如下:

Model层(userlist):using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace Model
{public class userlist{public string id { get; set; }public string username { get; set; }public string userpwd { get; set; }public string realname { get; set; }public string iphone { get; set; }public string flge { get; set; }}}

先在Model里面实例化对象。

DAL层(diaoyong):using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;namespace DAL
{public class diaoyong{public DataSet Tabl(string where)   {DataSet ds = DB.ToGetData("select *from Table_4" + where);if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0){return ds;}else {return null;}}public class DB{static string ConnStr = "Data Source=.;Initial Catalog=调用的数据库名;Persist Security Info=True;User ID=数据库用户名;Password=数据库密码";public static DataSet ToGetData(string Sql){using (SqlConnection Conn = new SqlConnection(ConnStr)){using (SqlDataAdapter da = new SqlDataAdapter(Sql, Conn)){DataSet ds = new DataSet();Conn.Open();da.Fill(ds);da.Dispose();return ds;}}}}}
}

在DAL里面返回SQL里面的数据。

BLL(diaofang)层:using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;namespace BLL
{public class diaofang{DAL.diaoyong MyTestDal = new DAL.diaoyong();public DataSet Tabl(string where)   {return MyTestDal.Tabl("");}}
}

BLL层写入功能。

UI层(前端):<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AddUserList.aspx.cs" Inherits="三层.AddUserList" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server"><title></title>
</head>
<body><form id="form1" runat="server"><div><asp:Button ID="Button1" runat="server" Text="下载文件" onclick="Button1_Click" /></div></form>
</body>
</html>

前端加入一个按钮,后端调用方法。

UI层(后端):using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using System.Data;
using System.IO;namespace 三层
{public partial class AddUserList : System.Web.UI.Page{BLL.diaofang MyTestBll = new BLL.diaofang();protected void Button1_Click(object sender, EventArgs e){string filePath = @"E:\Excel\" + DateTime.Now.ToFileTime() + ".xls";DataSetToExcel(MyTestBll.Tabl(""), filePath);//以字符流的形式下载文件FileStream fs = new FileStream(filePath, FileMode.Open);byte[] bytes = new byte[(int)fs.Length];fs.Read(bytes, 0, bytes.Length);fs.Close();Response.ContentType = "application/octet-stream";//通知浏览器下载文件而不是打开Response.AddHeader("Content-Disposition", "attachment;  filename=" + HttpUtility.UrlEncode("downexcel.xls", System.Text.Encoding.UTF8));Response.BinaryWrite(bytes);Response.Flush();Response.End();}public void DataSetToExcel(DataSet ds, string FileName){try{FileStream fs = new FileStream(FileName, FileMode.Create, FileAccess.Write);StreamWriter sfw = new StreamWriter(fs, System.Text.Encoding.GetEncoding("gb2312"));//变量定义 //定义表对象与行对象,同时用DataSet对其值进行初始化 System.Data.DataTable dt = ds.Tables[0];DataRow[] myRow = dt.Select();int cl = dt.Columns.Count;string TableHtml = "";string TableHead = "用户编号,用户名,密码,真实姓名,手机号,状态";string TableTr = "";string TableTD = "";foreach (string item in TableHead.Split(',')){TableTD = TableTD + "<td>" + item + "</td>";}TableTr = TableTr + "<tr>" + TableTD + "</tr>";//sfw.WriteLine();//sfw.WriteLine(colHeaders);//逐行处理数据 foreach (DataRow row in myRow){TableTD = "";//当前数据写入 for (int i = 0; i < cl; i++){if (IsNumeric(row[i].ToString())){TableTD = TableTD + "<td>'" + row[i].ToString() + "</td>";}else{TableTD = TableTD + "<td>" + row[i].ToString() + "</td>";}}TableTr = TableTr + "<tr>" + TableTD + "</tr>";}string stylecss = "<style type='text/css'> ";stylecss = stylecss + " table.gridtable { ";stylecss = stylecss + " font-family: verdana,arial,sans-serif; ";stylecss = stylecss + " font-size:20px; ";stylecss = stylecss + " color:#333333; ";stylecss = stylecss + " border-width: 1px; ";stylecss = stylecss + "  border-color: #666666; ";stylecss = stylecss + " border-collapse: collapse; ";stylecss = stylecss + "  } ";stylecss = stylecss + "  table.gridtable th { ";stylecss = stylecss + "   border-width: 1px; ";stylecss = stylecss + "    padding: 8px; ";stylecss = stylecss + "     border-style: solid; ";stylecss = stylecss + "    border-color: #666666; ";stylecss = stylecss + "     background-color: #dedede; ";stylecss = stylecss + " } ";stylecss = stylecss + "  table.gridtable td { ";stylecss = stylecss + "      border-width: 1px; ";stylecss = stylecss + "      padding: 8px; ";stylecss = stylecss + "      border-style: solid; ";stylecss = stylecss + "      border-color: #666666; ";stylecss = stylecss + "     background-color: #ffffff; ";stylecss = stylecss + "  } </style>";TableHtml = stylecss + "<table class='gridtable'>" + TableTr + "</table>";sfw.Write(TableHtml);sfw.Close();}catch (Exception e){throw e;}}public static bool IsNumeric(string value){return Regex.IsMatch(value, @"^[+-]?\d*[.]?\d*$");}}}

string filePath = @“E:\Excel” + DateTime.Now.ToFileTime() + “.xls”;注意这一句话,需要在E盘下建立一个Excel文件夹,后面可以在里面找到Excel文件。地址也可以自己更改。

后端将数据一条条写入Excel里面,并存入指定位置。

成功以后是这个状态:

在文件夹里面自动命名

怎么样?会了吗?

使用Excel下载数据库里的数据相关推荐

  1. java 往excel中写数据库,poi将数据写入excel表格-怎么用java把数据库里的数据写入到excel表中...

    怎么用java把数据库里的数据写入到excel表中 你是想读取excel内容,然后整合一下数据,然后再生成一个新的excel吧 package aa; import java.io.FileInput ...

  2. mysql批量删除进程_小程序批量删除云数据库里的数据

    我们用云开发的云数据库存数据,难免会遇到数据过多,或者一些过时数据要删除的需求.之前云开发删除数据库只能一条条的删除.要想批量删除很麻烦,近期云开发推出了批量删除数据的方法.甚至可以稍微改造下实现数据 ...

  3. oracle数据库数据消失,,保存在数据库里的数据莫名其妙的消失

    求助,保存在数据库里的数据莫名其妙的消失 我做了一个批量修改的功能,数据是肯定存到数据库里了,提交给测试部测试也没什么BUG,可是当有别的classes文件替换之后 重启服务器,我之前修改的数据就会莫 ...

  4. 如何获取公开数据库里的数据

    1. 用代码获取数据 当然下载数据的方式有多种多样,你可以用最原始的方法在数据库网站上点击下载,但是在不经意之间,电脑不仅帮你下载好了数据,还把原本压缩好的数据给自动解压缩了,原本小巧的压缩包瞬间变成 ...

  5. 通过JDBC连接数据库再向数据库里录入数据

    一  首先第一步我们现在数据库里创建一张examstudent空表: 二 接着对应着表里内容创建一个学生类: package com.atguigu.jdbc;public class Student ...

  6. 代码优化从数据库里查数据

    今天写了几行代码,都是从一个表里查数据.而我却查了三次数据库, 代码例子如下: dalclass.GetLie("userName","Student",&qu ...

  7. 如何使用 Node.js 访问 SAP HANA Cloud 数据库里的数据

    登录 SAP Business Technology Platform,找到 space 下自己创建好的 HANA Cloud 实例,右键菜单选择 Copy SQL Endpoint,将 HANA C ...

  8. java中excel文件导入数据库中_〖JAVE经验〗java中Excel导入数据库里

    1 从Excel文件读取数据表 Java Excel API既可以从本地文件系统的一个文件(.xls),也可以从输入流中读取Excel数据表.读取Excel数据表的第一步是创建Workbook(术语: ...

  9. jTable保存到mysql_怎么把从数据库里的数据输到JTABLE里面

    int i=0; int column; Vector vcdata=new Vector();//使用容器 DefaultTableModel dtm=new DefaultTableModel() ...

最新文章

  1. 婚纱摄影小程序能带来订单吗?小程序如何做营销?
  2. PHP中htmlentities跟htmlspecialchars的区别
  3. JSTL解析——001
  4. 复现强网杯python is the best language 2
  5. HDU - 5572 An Easy Physics Problem(几何-碰撞问题)
  6. 如何将原图和json融合_图像语义分割出的json文件和原图,用plt绘制图像mask
  7. matlab ssgs工具箱,基于PI控制方式的1A开关电源MATLAB仿真研究
  8. scpi指令转换c语言,[C#源代码]使用SCPI指令对通讯端口(RS232/USB/GPIB/LAN)进行仪器编程...
  9. 【matplotlib笔记】3D图像绘制
  10. java steam_Java,Steam控制器和我
  11. Django 开发的个人博客源码分享
  12. 【黑帽SEO系列】网页劫持
  13. HDU3338Kakuro Extension(最大流+边的流量)
  14. 2020年,这个算法团队都干了啥?
  15. CiteSpace知识图谱
  16. 改善睡眠失眠10大方法,让你失眠一招入睡
  17. 服务器资产管理条码系统,资产条码管理系统
  18. 递归函数求解阶层(C语言)
  19. idea中创建并使用自定义maven模板
  20. superset设置起止时间为明天

热门文章

  1. html分屏模板,使用 JavaScript 实现分屏视觉效果
  2. 在哪里可以下载免费的office使用?
  3. EMD EEMD CEEMD对应的matlab工具包
  4. linux 替换文件中的字符串——sed
  5. 校招 | 微软萌新记:实习初体验
  6. matlab中将灰度图像转换为彩色图像
  7. Linux 蓝牙读写,[BlueZ] 2、使用bluetoothctl搜索、连接、配对、读写、使能notify蓝牙低功耗设备...
  8. 黄大叔学MPC系列之Lyapunov-based Nonlinear MPC
  9. 【华人学者风采】周裕 哈尔滨工业大学深圳研究院
  10. 算法器之AVR的ISP烧录