簡體   English   中英

使用EPPlus C#無法將數據從Datagridview插入Excel

[英]Failed to Insert Data from Datagridview to Excel using EPPlus C#

將數據插入Excel時遇到一些問題。 我想使用C#將datagridview中顯示的數據插入Excel文件。 我的代碼沒有顯示任何錯誤,但是當我檢查excel文件時,沒有輸入任何數據。 誰能幫我 ? 我使用EPPlus

string lokasifile = @"D:\\Data Pengisian SLA Surabaya\\" + new System.Globalization.CultureInfo("id-ID").DateTimeFormat.GetDayName(DateTime.Now.DayOfWeek) + "_" + System.DateTime.Now.Date.ToString("dd MMM yyyy", new System.Globalization.CultureInfo("id-ID")) + ".xlsx";


private void PrintScheduleBtn_Click(object sender, EventArgs e)
        {

            if (StaffATB.Text != "" && HelperTeamATB.Text != "" && StaffBTB.Text != "" && HelperTeamBTB.Text != "" && StaffCTB.Text != "" && HelperTeamCTB.Text != "" && StaffDTB.Text != "" && HelperTeamDTB.Text != "")
            {
                DialogResult dialogResult = MessageBox.Show("Apakah Anda yakin ingin menyimpan jadwal pengisian ?", "", MessageBoxButtons.YesNo);

                if (dialogResult == DialogResult.Yes)
                {
                    ExcelPackage pck = new ExcelPackage();
                    FileStream stream = new FileStream(lokasifile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

                    pck.Load(stream);

                    var rekap = pck.Workbook.Worksheets["Rekap"];

                    OleDbConnection kon = new OleDbConnection(konekpengisian2);
                    OleDbCommand command = kon.CreateCommand();
                    OleDbCommand command1 = kon.CreateCommand();
                    OleDbCommand command2 = kon.CreateCommand();
                    OleDbCommand command3 = kon.CreateCommand();

                    kon.Open();

                    int tima = 0;
                    int timb = 0;
                    int timc = 0;
                    int timd = 0;
                    int time = 0;

                    foreach (DataGridViewRow row in JadwalisiGV.Rows)
                    {
                        if (!row.IsNewRow)
                        {
                            if (row.Cells["Tim"].Value.ToString() == "A")
                            {
                                tima++;
                                rekap.Cells[tima + 7, 3].Value = row.Cells["WSID"].Value;
                                rekap.Cells[tima + 7, 4].Value = row.Cells["Limit"].Value + ",000,000";
                                IsiTimABox.Text = tima.ToString();
                            }
                            if (row.Cells["Tim"].Value.ToString() == "B")
                            {
                                timb++;
                                rekap.Cells[timb + 7, 9].Value = row.Cells["WSID"].Value;
                                rekap.Cells[timb + 7, 10].Value = row.Cells["Limit"].Value + ",000,000";
                                IsiTimBBox.Text = timb.ToString();
                            }
                            if (row.Cells["Tim"].Value.ToString() == "C")
                            {
                                timc++;
                                rekap.Cells[timc + 28, 3].Value = row.Cells["WSID"].Value;
                                rekap.Cells[timc + 28, 4].Value = row.Cells["Limit"].Value + ",000,000";
                                IsiTimCBox.Text = timc.ToString();
                            }
                            if (row.Cells["Tim"].Value.ToString() == "D")
                            {
                                timd++;
                                rekap.Cells[timd + 28, 9].Value = row.Cells["WSID"].Value;
                                rekap.Cells[timd + 28, 10].Value = row.Cells["Limit"].Value + ",000,000";
                                IsiTimDBox.Text = timd.ToString();
                            }
                            if (row.Cells["Tim"].Value.ToString() == "E")
                            {
                                time++;
                                rekap.Cells[time + 7, 15].Value = row.Cells["WSID"].Value;
                                rekap.Cells[time + 7, 16].Value = row.Cells["Limit"].Value + ",000,000";
                                IsiTimEBox.Text = time.ToString();
                            }

                            TotalMesinBox.Text = (tima + timb + timc + timd + time).ToString();

                        }
                    }

                    kon.Close();

                }


            }
            else
            {
                MessageBox.Show("Silakan isi PIC terlebih dahulu !");
            }

        }

當您使用文件流打開包時, Save()方法不會回寫您所做的任何更改。 它無聲地失敗。 如果不是這樣,則仍然會失敗,因為使用FileAccess.Read打開了流,這會阻止對基礎文件的更改。

要糾正這些問題並確保清理干凈,請按如下所示很好地修改您的代碼:

private void PrintScheduleBtn_Click(object sender, EventArgs e)
{

    if (StaffATB.Text != "" && HelperTeamATB.Text != "" && StaffBTB.Text != "" && HelperTeamBTB.Text != "" && StaffCTB.Text != "" && HelperTeamCTB.Text != "" && StaffDTB.Text != "" && HelperTeamDTB.Text != "")
    {
        DialogResult dialogResult = MessageBox.Show("Apakah Anda yakin ingin menyimpan jadwal pengisian ?", "", MessageBoxButtons.YesNo);

        if (dialogResult == DialogResult.Yes)
        {
            // create an FileInfo instance
            var file = new FileInfo(lokasifile);
            // use the contructor that take an FileInfo
            // wrap in a using so stuff gets disposed nicely
            using (ExcelPackage pck = new ExcelPackage(file))
            {
                var rekap = pck.Workbook.Worksheets[1];

                // the rest of your code here

                // the rest of your code is above
                // explicitely Save the changes
                pck.Save();
            }
        }
    }
}

暫無
暫無

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

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