基本信息
源码名称:asp.net 将数据导出到excel文件(NPOI)
源码大小:13.29M
文件格式:.zip
开发语言:C#
更新时间:2019-06-28
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
错误异常抛出框架
错误导入导出功能
excel的内容如下:
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NPOIDemo;
namespace NPOIDemo
{
public partial class Main : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected DataTable GenerateData()
{
DataTable data = new DataTable();
for (int i = 0; i < 5; i)
{
data.Columns.Add("Columns_" i.ToString(), typeof(string));
}
for (int i = 0; i < 10; i)
{
DataRow row = data.NewRow();
row["Columns_0"] = "item0_" i.ToString();
row["Columns_1"] = "item1_" i.ToString();
row["Columns_2"] = "item2_" i.ToString();
row["Columns_3"] = "item3_" i.ToString();
row["Columns_4"] = "item4_" i.ToString();
data.Rows.Add(row);
}
return data;
}
protected void TestExcelWrite(string file)
{
try
{
using (ExcelHelper excelHelper = new ExcelHelper(file))
{
DataTable data = GenerateData();
int count = excelHelper.DataTableToExcel(data, "MySheet", true);
if (count > 0)
Console.WriteLine("Number of imported data is {0} ", count);
}
}
catch (Exception ex)
{
Console.WriteLine("Exception: " ex.Message);
}
}
protected void DownLoad (string fileName,string filePath)
{
Response.ContentType = "application/x-zip-compressed";
Response.AddHeader("Content-Disposition", "attachment;filename=" fileName);
string filename = filePath;
Response.TransmitFile(filename);
}
protected void Button1_Click(object sender, EventArgs e)
{
string file = @"~/temp/myTest.xlsx";
TestExcelWrite(Server.MapPath(file));
DownLoad("myTest.xlsx", Server.MapPath(file));
}
}
}