我正在尝试List<string>
使用 EPPlus 将值从 a 保存到 Excel 工作表,所以我编写了以下代码:
private void button3_Click(object sender, EventArgs e)
{
int value = bdCleanList.Count() / Int32.Parse(textBox7.Text);
string bases_generadas = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bases_generadas");
var package = new ExcelPackage();
package.Workbook.Worksheets.Add("L1");
ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
worksheet.Name = "L1";
int j = 2;
int col = 1;
for (int i = 1; i < bdCleanList.Count(); i++)
{
if (i%Int32.Parse(textBox7.Text) == 0)
{
package.Workbook.Worksheets.Add("L" + j);
worksheet = package.Workbook.Worksheets[j];
worksheet.Name = "L" + j;
j += 1;
worksheet.Cells[i, col].Value = bdCleanList[i];
}
else
{
worksheet.Cells[i, col].Value = bdCleanList[i];
}
}
Byte[] bin = package.GetAsByteArray();
File.WriteAllBytes(System.IO.Path.Combine(bases_generadas, "bases_generadas_" + DateTime.Now.Ticks.ToString() + DateTime.Now.ToString("dd-MM-yyyy-hh-mm-ss") + ".xlsx"), bin);
MessageBox.Show("Se generaron un total de " + value + " bases y puede encontrarlas en la siguiente ruta: " + System.IO.Path.Combine(bases_generadas, "bases_generadas_" + DateTime.Now.Ticks.ToString() + DateTime.Now.ToString("dd-MM-yyyy-hh-mm-ss") + ".xlsx"), "Información", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
在我运行的示例bdCleanList.Count()
中有 2056 个值, Int32.Parse(textBox7.Text) 有 500 作为值,所以value
在这种情况下5,这里的问题是 L2, L3 ... L5 的值没有保存,我不知道为什么。第一个工作表的值保存得很好,但其余的没有,我的代码有什么问题?如何设置活动工作表以将值保存在活动工作表上?如何在工作表之间移动?