C#利用Aspose在同一个Excel文件中创建多个sheet

代码如下:

        private void DataGridViewExportSheet(Aspose.Cells.Worksheet sheet, DataGridView dataGridView)
        {
            int irows = dataGridView.RowCount;
            int icols = dataGridView.ColumnCount;

            for (int i = 0; i < icols; i++)
            {
                if (dataGridView.Columns[i].Visible)
                {
                    string fldName = dataGridView.Columns[i].HeaderText;
                    sheet.Cells[0, i].PutValue(fldName);
                }
            }

            for (int i = 0; i < irows; i++)
            {
                for (int j = 0; j < icols; j++)
                {
                    if (dataGridView.Columns[j].Visible)
                    {
                        if (dataGridView.Rows[i].Cells[j].Value != null)
                        {
                            sheet.Cells[i + 1, j].PutValue(dataGridView.Rows[i].Cells[j].Value.ToString());
                        }
                    }
                }
            }
        }

调用方法:

  new Aspose.Cells.License().SetLicense(new MemoryStream(Convert.FromBase64String(Key)));
  workbook = new Aspose.Cells.Workbook();
  Aspose.Cells.Worksheet sheet = workbook.Worksheets[0];
  sheet.Name = "基本信息";
  DataGridViewExportSheet(sheet, dgvzkxx);
  workbook.Save(strpath);
本博客有部分内容来自网络,如有问题请联系:hebeilijianghua@qq.com,并注明来自博客园。
原文地址:https://www.cnblogs.com/leebokeyuan/p/14680431.html