c# - 用 C# 填充 excel 表

标签 c# excel data-structures

我的程序结构出了问题。每当我在我的 Windows 窗体应用程序中按下一个按钮时,我的 Excel 工作表应该用数据更新一个单元格。

这是我的代码:

using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
namespace Test6attendance
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {


        Excel.Application xlApp;
        Excel.Workbook xlWorkBook;
        Excel.Worksheet xlWorkSheet;

        if (File.Exists("D:\\login.xlscsharp-Excel.xls"))
        {

            object misValue = System.Reflection.Missing.Value;
            xlApp = new Excel.Application();
            xlWorkBook = xlApp.Workbooks.Open("D:\\login.xlscsharp-Excel.xls", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);

            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            for (int i = 1; i < 55555; i++)
            {


                if (xlWorkSheet.Cells[i, 1] == null)
                {

                    xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
                    xlWorkSheet.Cells[i, 1] = DateTime.Now.ToString("MM/dd/ yyyy, hh:mm:ss tt");
                    xlWorkBook.SaveAs("D:\\login.xlscsharp-Excel.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
                    xlWorkBook.Close(true, misValue, misValue);
                    xlApp.Quit();

                    releaseObject(xlWorkSheet);
                    releaseObject(xlWorkBook);
                    releaseObject(xlApp);

                    MessageBox.Show("Excel file updated , you can find the file D:\\csharp-Excel.xls");

                }

            }



        }
        else
        {

            //Create New Code



            object misValue = System.Reflection.Missing.Value;


            xlApp = new Excel.Application();
            xlWorkBook = xlApp.Workbooks.Add(misValue);




            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            xlWorkSheet.Cells[1, 1] = DateTime.Now.ToString("MM/dd/ yyyy, hh:mm:ss tt");


            xlWorkBook.SaveAs("D:\\login.xlscsharp-Excel.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
            xlWorkBook.Close(true, misValue, misValue);
            xlApp.Quit();

            releaseObject(xlWorkSheet);
            releaseObject(xlWorkBook);
            releaseObject(xlApp);

            MessageBox.Show("Excel file created , you can find the file D:\\csharp-Excel.xls");

        }
    }
         private void releaseObject(object obj)
    {
        try
        {
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
            obj = null;
        }
        catch (Exception ex)
        {
            obj = null;
            MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
        }
        finally
        {
            GC.Collect();
        }
    }

    private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
    {

    }
    }


}

我认为问题出在 for 循环上,它似乎不起作用。需要一些建议或引用来修复它。

最佳答案

问题出在

if (xlWorkSheet.Cells[i, 1] == null)

应该是

 if (xlWorkSheet.Cells[i, 1] == null)

由于您的第一个 if 条件检查文件是否存在 if (File.Exists("D:\\login.xlscsharp-Excel.xls")),如果不存在则创建一个文件并分配单元格 [1,1] 的值。现在,当您第二次单击时,单元格不为空并且您的值不会更新。

试试这段代码

for (int i = 1; i < 55555; i++)
            {


                if (xlWorkSheet.Cells[i, 1] != null)
                {

                    xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
                    xlWorkSheet.Cells[i, 1] = DateTime.Now.ToString("MM/dd/ yyyy, hh:mm:ss tt");
                    xlWorkBook.SaveAs("D:\\login.xlscsharp-Excel.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
                    xlWorkBook.Close(true, misValue, misValue);
                    xlApp.Quit();

                    releaseObject(xlWorkSheet);
                    releaseObject(xlWorkBook);
                    releaseObject(xlApp);

                    MessageBox.Show("Excel file updated , you can find the file D:\\csharp-Excel.xls");
                    break;

                }

            }

关于c# - 用 C# 填充 excel 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23928146/

相关文章:

c# - 为什么在 C# 条件运算符时出现堆栈溢出异常?

c# - 在 ObservableCollection 的特定元素上绑定(bind) DataGrid

c# - 从 KeyedCollection 获取键列表的最有效方法是什么?

算法/数据结构——查找数组中小于给定数字的连续数字之间的最大差异

c# - 接受空 Lambda 表达式的函数的定义原型(prototype)

c# - 使用 Excel-DNA,如何区分 Excel 加载项函数调用中未指定的参数和缺失的参数?

C# System.__访问Excel文件时返回ComObject

vba - Excel VBA - 将带有文本和格式的新列添加到多个工作表

javascript - 如何根据数组属性的索引拆分和合并javascript对象

c# - 如何将图像控件绑定(bind)到以 VarBinary 类型保存在数据库中的图像