簡體   English   中英

使用C#填充excel表

[英]Populate excel sheet with C#

我的程序結構出錯了。 每當我在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")) ,如果沒有,則它創建一個文件並將值賦給cell [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;

                }

            }

你的循環很奇怪。 您應該將所有代碼放在循環外部的循環中。 您應該留下的唯一內容是您分配值的行。 否則,您為保存的每一行保存應用程序。 更不用說在分配第一個值后釋放所有對象。 我猜這只能寫一個值,然后在for循環的第一行引發許多異常(你試圖從你已經發布的工作簿對象中獲取工作表)。

它應該是這樣的:

for (int i = 1; i < 55555; i++)
{
   if (xlWorkSheet.Cells[i, 1] != null)
   {
       xlWorkSheet.Cells[i, 1] = DateTime.Now.ToString("MM/dd/ yyyy, hh:mm:ss tt"); 
   }
}
xlWorkBook.Save("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");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM