代码如下:
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);