ASP.NET从数据库导出到EXCEL

excel

1个回答

写回答

朱大帅666

2026-01-30 16:13

+ 关注

excel
excel

要将ASP.NET网站中的数据导出到excel文件中,可以使用Microsoft Office Interop组件。以下是一个简单的示例:csharpusing System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Text;using System.Threading.Tasks;using Microsoft.Office.Interop.excel;using Microsoft.Office.Interop.excel.Coerce;using Microsoft.Office.Interop.excel.Workbook;public static void ExportToexcel(string connectionString, string fileName) { // Create a new excel application using (var excelApp = new Application()) { // Create a new workbook var workbook = new Workbook(excelApp); // Connect to the Database var connection = new ConnectionStrings().Open(connectionString); var connectionSource = new ConnectionSource(connection); var worksheet = workbook.Worksheets.Add("Sheet1"); // Loop through all rows in the Database and add them to the worksheet var rowCount = connectionSourceRodCount; for (int i = 1; i

举报有用(0分享收藏

Copyright © 2025 IZhiDa.com All Rights Reserved.

知答 版权所有 粤ICP备2023042255号