当前位置:网站首页>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表格对应的列中
边栏推荐
- typescript54 - generic constraints
- C# WPF设备监控软件(经典)-下篇
- C语言 函数递归
- 关于mnn模型输出的数据杂乱无章问题
- 通过whl安装第三方包
- 一文参透分布式存储系统Ceph的架构设计、集群搭建(手把手)
- Justin Sun: Web3.0 and the Metaverse will assist mankind to enter the online world more comprehensively
- 扩展卡尔曼滤波EKF
- Talking about the future development direction of my country's industrial parks
- Install third-party packages via whl
猜你喜欢

轻量级网络整理及其在Yolov5上的实现

网络带宽监控,带宽监控工具哪个好

全面讲解 Handler机制原理解析 (小白必看)

ML18-自然语言处理

typescript52 - simplify generic function calls

Eight things to pay attention to in spot silver
![[Miscellaneous] How to install the specified font into the computer and then use the font in the Office software?](/img/15/23b0724f9c9672c61b91320f1b84d8.png)
[Miscellaneous] How to install the specified font into the computer and then use the font in the Office software?

typescript55-泛型约束

分布式事务框架 seata

现货白银需要注意八大事项
随机推荐
typescript56-泛型接口
教你如何定位不合理的SQL?并优化之
身为程序员的我们如何卷死别人?破局重生。
Tanabata festival coming, VR panoramic look god assists for you
[Miscellaneous] How to install the specified font into the computer and then use the font in the Office software?
boot issue
米哈游--测试开发提前批
关于mnn模型输出的数据杂乱无章问题
typescript52-简化泛型函数调用
做项目一定用得到的NLP资源【分类版】
114. How to find the cause of Fiori Launchpad routing error by single-step debugging
Apache DolphinScheduler新一代分布式工作流任务调度平台实战-中
Install third-party packages via whl
手撕Nacos源码,今日撕服务端源码
nodeJs--async/await
伦敦银最新均线分析系统怎么操作?
七夕活动浪漫上线,别让网络拖慢和小姐姐的开黑时间
What warehouse management problems can WMS warehouse management system solve in the electronics industry?
共享新能源充电桩充电站建设需要些什么流程及资料?
Demand analysis of MES management system in electronic assembly industry