当前位置:网站首页>ASP.NET 获取数据库的数据并写入到excel表格中
ASP.NET 获取数据库的数据并写入到excel表格中
2022-08-04 00:47:00 【CSDN问答】
问题遇到的现象和发生背景
从数据库读取出数据,并且把数据写入到excel表格对应的列中
问题相关代码,请勿粘贴截图
string fileDir = System.Web.HttpContext.Current.Server.MapPath("~/UI/BQ/Temp/测试报表.xlsx"); FileStream file = new FileStream(fileDir, FileMode.Open, FileAccess.Read); //创建HSSFWorkbook对象 XSSFWorkbook hssfworkbook = new XSSFWorkbook(file); //创建HSSFSheet对象 NPOI.SS.UserModel.ISheet sheet = hssfworkbook.GetSheetAt(0); //ISheet sheet = hssfworkbook.GetSheet("sheet1"); System.Collections.IEnumerator rows = sheet.GetRowEnumerator(); DataTable dataTable; CustomSqlSection customSqlSection = Gateway.Default.FromCustomSql(sql); dataTable = customSqlSection.ToDataSet().Tables[0]; for (int i = 2; i < dataTable.Rows.Count; i++) { sheet.GetRow(i).GetCell(0).SetCellValue(dataTable.Rows[i - 2]["VIN"].ToString()); sheet.GetRow(i).GetCell(1).SetCellValue(dataTable.Rows[i - 2]["CAR_TYPE_CODE"].ToString()); sheet.GetRow(i).GetCell(2).SetCellValue(dataTable.Rows[i - 2]["BAD_DESC"].ToString()); sheet.GetRow(i).GetCell(3).SetCellValue(dataTable.Rows[i-2]["Model"].ToString()); sheet.GetRow(i).GetCell(4).SetCellValue(dataTable.Rows[i - 2]["LEVEL_VAL"].ToString()); sheet.GetRow(i).GetCell(5).SetCellValue(dataTable.Rows[i - 2]["HD2"].ToString()); sheet.GetRow(i).GetCell(6).SetCellValue(dataTable.Rows[i - 2]["OTHER_DES"].ToString()); }
运行结果及报错内容
写入的时候这个循环体内的数据报错: sheet.GetRow(i).GetCell(0).SetCellValue(dataTable.Rows[i - 2]["VIN"].ToString());//
{"EXCEPTION":"文件写入数据异常:未将对象引用设置到对象的实例。"}
我的解答思路和尝试过的方法
给表格增加列名
我想要达到的结果
从数据库读取出数据,并且把数据写入到excel表格对应的列中
边栏推荐
猜你喜欢
jmeter跨平台运行csv等文件
Justin Sun was invited to attend the 36氪 Yuan Universe Summit and delivered a keynote speech
查看CUDA、pytorch等的版本号
面试必问的HashCode技术内幕
corn表达式 具体详解与案例
"Miscellaneous" barcode by Excel as a string
Demand analysis of MES management system in electronic assembly industry
浅谈我国产业园区未来的发展方向
c语言分层理解(c语言指针(上))
nodeJs--async/await
随机推荐
Apple told Qualcomm: I bought a new campus for $445 million and may plan to speed up self-development of baseband chips
typescript55-泛型约束
通过whl安装第三方包
BioVendor人Clara细胞蛋白(CC16)Elisa试剂盒检测步骤
无代码7月热讯 | 微软首推数字联络中心平台;全球黑客马拉松...
查看CUDA、pytorch等的版本号
【链路聚合原理及配置】
GeoAO:一种快速的环境光遮蔽方案
新一代服务网关Gateway的实践笔记
2022-08-03:以下go语言代码输出什么?A:2;B:3;C:1;D:0。 package main import “fmt“ func main() { slice := []i
MPLS综合实验
2022年8月份DAMA-CDGA/CDGP数据治理认证招生简章
typescript58 - generic classes
XSS - Bypass for loop filtering
跨域问题解决方式 代理服务器
做项目一定用得到的NLP资源【分类版】
利用matlab求解线性优化问题【基于matlab的动力学模型学习笔记_11】
XSS-绕过for循环过滤
typescript56-泛型接口
Apache DolphinScheduler新一代分布式工作流任务调度平台实战-中