C# Winform里面输出数据到Excel的问题

本文介绍如何使用C#和Microsoft Office Interop Excel库来创建Excel工作表,并演示了如何填充特定范围内的单元格。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;

using System.Reflection

using Microsoft.Office.Interop.Excel;



public class CreateExcelWorksheet

{

    static 
void Main()

    {

        
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();



        if (
xlApp == null)

        {

            
Console.WriteLine("EXCEL could not be started. Check that your office installation and project references are correct.");

            return;

        }

        
xlApp.Visible true;



        
Workbook wb xlApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);

        
Worksheet ws = (Worksheet)wb.Worksheets[1];



        if (
ws == null)

        {

            
Console.WriteLine("Worksheet could not be created. Check that your office installation and project references are correct.");

        }



        
// Select the Excel cells, in the range c1 to c7 in the worksheet.

        
Range aRange ws.get_Range("C1""C7");



        if (
aRange == null)

        {

            
Console.WriteLine("Could not get a range. Check to be sure you have the correct versions of the office DLLs.");

        }



        
// Fill the cells in the C1 to C7 range of the worksheet with the number 6.

        
Object[] args = new Object[1];

        
args[0] = 6;

        
aRange.GetType().InvokeMember("Value"BindingFlags.SetPropertynullaRangeargs);

    

        
// Change the cells in the C1 to C7 range of the worksheet to the number 8.

        
aRange.Value2 8;

    }

}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值