第一种:打开微信,搜一搜"别打我女儿的主意"打开微信小程序,找到菜单栏,点击源码,进去就可以获得链接
第二种:可以给本文点赞、好评,然后发邮件到[email protected],如果有学习资料视频可以分享的话,可以捎带分享给我。
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ExportImportExecl
{
public partial class index : System.Web.UI.Page
{
EntitiesModel db = new EntitiesModel();
protected void Page_Load(object sender, EventArgs e)
{
}
//导出学生信息
protected void btn_Export_Click(object sender, EventArgs e)
{
string fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".xls"; // 文件名称
string urlPath = "upload/execl/" + fileName; // 文件下载的URL地址,供给前台下载
string filePath = HttpContext.Current.Server.MapPath("\\" + urlPath); // 文件路径
// 1.检测是否存在文件夹,若不存在就建立个文件夹
string DirectoryName = Path.GetDirectoryName(filePath);
if (!Directory.Exists(DirectoryName))
{
Directory.CreateDirectory(DirectoryName);
}
try
{
DataTable dt = new DataTable();
DataColumn dc = new DataColumn();
dc = dt.Columns.Add("id", typeof(string)); //标识id
dc = dt.Columns.Add("name", typeof(string)); //姓名
dc = dt.Columns.Add("age", typeof(string)); //年龄
dc = dt.Columns.Add("sex", typeof(string)); //性别
dc = dt.Columns.Add("sort", typeof(string)); //排序
var stu_info = db.s_studentinfo.Where(x => x.id > 0);
foreach (var item in stu_info)
{
DataRow dr = dt.NewRow();
dr["id"] = item.id.ToString(); //标识id
dr["name"] = item.name.ToString(); //姓名
dr["age"] = item.age.ToString(); //年龄
dr["sex"] = item.sex.ToString(); //性别
dr["sort"] = item.sort.ToString(); //排序
dt.Rows.Add(dr);
}
string[] s = new string[] { "标识id", "姓名", "年龄", "性别", "排序" };
HSSFWorkbook workbook = DownExcel(s, dt);
FileStream file = new FileStream(filePath, FileMode.Create);
workbook.Write(file);
file.Close();
string A = urlPath;
}
catch (Exception)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "");
}
}
#region 生成Excel ================================================================
///
/// 生成Excel
///
/// excel第一行的数据(列名)
/// table
///
public HSSFWorkbook DownExcel(string[] s, DataTable dt)
{
HSSFWorkbook workbook = new HSSFWorkbook();
ISheet sheet = workbook.CreateSheet("sheet1");
for (int i = 0; i < s.Length; i++)
{
if (i == 0)
{
sheet.CreateRow(0).CreateCell(i).SetCellType(CellType.STRING);
sheet.CreateRow(0).CreateCell(i).SetCellValue(s[i]);
}
else
{
sheet.GetRow(0).CreateCell(i).SetCellType(CellType.STRING);
sheet.GetRow(0).CreateCell(i).SetCellValue(s[i]);
}
}
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < s.Length; j++)
{
if (j == 0)
{
sheet.CreateRow(i + 1).CreateCell(j).SetCellType(CellType.STRING);
sheet.CreateRow(i + 1).CreateCell(j).SetCellValue(dt.Rows[i][j].ToString());
}
else
{
sheet.GetRow(i + 1).CreateCell(j).SetCellType(CellType.STRING);
sheet.GetRow(i + 1).CreateCell(j).SetCellValue(dt.Rows[i][j].ToString());
}
}
}
return workbook;
}
#endregion
}
}