C#和EPPlus,将文本值从表单写入到下一个可用的空行

时间:2019-03-04 12:42:57

标签: c# loops visual-studio-2017 epplus

所以我正在使用EPPlus和C#,并且我有一个带有一些文本字段的表单,并且试图将文本字段值添加到下一个可用的空行中,但遇到了一些麻烦。我尝试了几种不同的方法来查找下一个可用的空行,以将文本字段值附加到该行,因为它具有列标题,因此总是跳过第一行。

我最接近的是使它不断写入第2行和第3行,但它始终会写入相同的信息。

//create an instance of the first sheet in the loaded file
ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets["Sheet1"];

var r = worksheet.Dimension.End.Row;
for (int i = 1; i <= r; i++)
{
     worksheet.Cells[i, 1].Value = comboBox1.Text;
     worksheet.Cells[i, 2].Value = textBox1.Text;
     worksheet.Cells[i, 3].Value = textBox2.Text;
     worksheet.Cells[i, 4].Value = textBox3.Text;
     worksheet.Cells[i, 5].Value = textBox4.Text;
     worksheet.Cells[i, 6].Value = richTextBox1.Text;
}

这里是我的所有代码,以防您需要查看我的工作方式或工作方式。

using System;
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 OfficeOpenXml;
using System.IO;
using OfficeOpenXml.Style;

namespace NetworkManager
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();            
        }

        public void Form1_Load(object sender, EventArgs e)
        {
            StreamReader sr = new StreamReader(@".\Owners.txt");
            string x = sr.ReadToEnd();
            string[] y = x.Split('\n');
            foreach (string s in y)
            {
                comboBox1.Items.Add(s);
            }
        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Create a new blank package/file
            using (var package = new ExcelPackage()) 
            {
                // Add a new worksheet to the empty workbook
                ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Sheet1");

                // Set the header text 
                worksheet.HeaderFooter.OddHeader.CenteredText = "&24&U&\"Arial,Regular Bold\" Router IP" + textBox5;

                //Set column width
                worksheet.Column(1).Width = 20;
                worksheet.Column(2).Width = 25;
                worksheet.Column(3).Width = 20;
                worksheet.Column(4).Width = 25;
                worksheet.Column(5).Width = 25;
                worksheet.Column(6).Width = 25;

                // Border and Font for Column Headers
                worksheet.Cells["A1:F1"].Style.Border.BorderAround(ExcelBorderStyle.Thick);  // Thick border around Column Headers
                worksheet.Cells["A1:F1"].Style.Font.Bold = true; // Bold text for Column Headers
                worksheet.Cells["A1:F1"].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;


                //Add column headers
                worksheet.Cells[1, 1].Value = "Owner";
                worksheet.Cells[1, 2].Value = "Device Name";
                worksheet.Cells[1, 3].Value = "Ip Address";
                worksheet.Cells[1, 4].Value = "Mac Address 1";
                worksheet.Cells[1, 5].Value = "Mac Address 2";
                worksheet.Cells[1, 6].Value = "Notes";


                // set some document properties
                package.Workbook.Properties.Title = "NetworkManager";
                package.Workbook.Properties.Author = "David Goodwin";
                package.Workbook.Properties.Comments = "This is a program to help manager your network addresses";

                //Save your file
                FileInfo fi = new FileInfo(@".\ExportedDoc\NetworkManager.xlsx");
                package.SaveAs(fi);

                MessageBox.Show("New File Created!");
            }
        }

        private void loadToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e) 
        {

        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void button1_Click(object sender, EventArgs e)  // Append data to already existing excel document
        {
            //the path of the file
            string filePath = ".\\ExportedDoc\\NetworkManager.xlsx";

            //create a fileinfo object of an excel file on the disk
            FileInfo file = new FileInfo(filePath);

            //create a new Excel package from the file
            using (ExcelPackage excelPackage = new ExcelPackage(file))
            {
                //create an instance of the first sheet in the loaded file
                ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets["Sheet1"];



                var r = worksheet.Dimension.End.Row;
                for (int i = 1; i <= r; i++)
                {
                    worksheet.Cells[i, 1].Value = comboBox1.Text;
                    worksheet.Cells[i, 2].Value = textBox1.Text;
                    worksheet.Cells[i, 3].Value = textBox2.Text;
                    worksheet.Cells[i, 4].Value = textBox3.Text;
                    worksheet.Cells[i, 5].Value = textBox4.Text;
                    worksheet.Cells[i, 6].Value = richTextBox1.Text;
                }


                /*
                // check to find last empty row to add new rows.
                int rowCount = worksheet.Dimension.End.Row;
                int colCount = worksheet.Dimension.End.Column;
                for (int row = 1; row <= rowCount; row++)
                {
                    // Create a bool
                    bool RowIsEmpty = true;

                    for (int col = 1; col <= colCount; col++)
                    {
                        // check if the cell is empty or not
                        if (worksheet.Cells[row, col].Value != null)
                        {
                            RowIsEmpty = false;
                        }
                    }

                    // if row is empty, hit flag and write data 
                    if (RowIsEmpty == true)
                    {
                        MessageBox.Show("Test");
                        // write data to next available row
                        int i = 1;
                        foreach (var item in worksheet.Cells)
                        {
                            worksheet.Cells[1, i].Value = comboBox1.Text;
                            worksheet.Cells[2, i].Value = textBox1.Text;
                            worksheet.Cells[3, i].Value = textBox2.Text;
                            worksheet.Cells[4, i].Value = textBox3.Text;
                            worksheet.Cells[5, i].Value = textBox4.Text;
                            worksheet.Cells[6, i].Value = richTextBox1.Text;
                            i++;
                        }
                    }
                }
                */
                /*
                // add some data
                worksheet.Cells["A2"].Value = comboBox1.Text;
                worksheet.Cells["B2"].Value = textBox1.Text;
                worksheet.Cells["C2"].Value = textBox2.Text;
                worksheet.Cells["D2"].Value = textBox3.Text;
                worksheet.Cells["E2"].Value = textBox4.Text;
                worksheet.Cells["F2"].Value = richTextBox1.Text;
                */

                // after adding the items, clear the fields                
                textBox1.Clear();
                textBox2.Clear();
                textBox3.Clear();
                textBox4.Clear();
                textBox5.Clear();
                richTextBox1.Clear();


                //save the changes
                excelPackage.Save();
            }
        }
    }
}

所以这似乎行得通,但是我不确定它是否有效。我还通过手动删除中间的一行进行了测试,它仍然可以到达列的末尾,这很好,我可以考虑删除第1行和rowCount之间的空白行。

//Cells only contains references to cells with actual data
int rowCount = worksheet.Dimension.End.Row;
int colCount = worksheet.Dimension.End.Column;
var cells = worksheet.Cells;
var rowIndicies = cells
    .Select(c => c.Start.Row)
    .Distinct()
    .ToList();

//Skip the header row
for (var i = 1; i <= rowCount; i++)                
{ 
    // add some data
    worksheet.Cells[rowCount + 1, 1].Value = comboBox1.Text;
    worksheet.Cells[rowCount + 1, 2].Value = textBox1.Text;
    worksheet.Cells[rowCount + 1, 3].Value = textBox2.Text;
    worksheet.Cells[rowCount + 1, 4].Value = textBox3.Text;
    worksheet.Cells[rowCount + 1, 5].Value = textBox4.Text;
    worksheet.Cells[rowCount + 1, 6].Value = richTextBox1.Text;
}

1 个答案:

答案 0 :(得分:0)

如果您要做的只是转到工作表的最底部,并使用Form控件中的值添加一行,那么您根本不需要for循环。这样的事情应该做到:

//Cells only contains references to cells with actual data
int rowCount = worksheet.Dimension.End.Row;
int colCount = worksheet.Dimension.End.Column;
var cells = worksheet.Cells;

var maxRow = cells
    .Select(c => c.Start.Row)
    .Max();

//Go to the next row after the max
maxRow++;
worksheet.Cells[maxRow, 1].Value = comboBox1.Text;
worksheet.Cells[maxRow, 2].Value = textBox1.Text;
worksheet.Cells[maxRow, 3].Value = textBox2.Text;
worksheet.Cells[maxRow, 4].Value = textBox3.Text;
worksheet.Cells[maxRow, 5].Value = textBox4.Text;
worksheet.Cells[maxRow, 6].Value = richTextBox1.Text;

让我知道我是否误会了。