111,128
社区成员
发帖
与我相关
我的任务
分享
DataGridViewRow row = new DataGridViewRow();
int index = dataGridView1.Rows.Add(row);
dataGridView1.Rows[index].Cells[0].Value = folderTime;
dataGridView1.Rows[index].Cells[1].Value = strName;
dataGridView1.Rows[index].Cells[2].Value = strID;
dataGridView1.Rows[index].Cells[3].Value = strGender;
dataGridView1.Rows[index].Cells[4].Value = Image.FromFile(imagePath1);
dataGridView1.Rows[index].Cells[5].Value = Image.FromFile(imagePath2);
int rowNum = dataGridView1.Rows.Count; //行数
int columnNum = dataGridView1.Columns.Count; //列数
int rowIndex = 0;
int columnIndex = 0;
if (rowNum != 0)
{
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
xlApp.Visible = false;
if (xlApp == null)
{
MessageBox.Show("无法创建excel对象");
return;
}
xlApp.DefaultFilePath = "";
xlApp.DisplayAlerts = true;
xlApp.SheetsInNewWorkbook = 1;
Microsoft.Office.Interop.Excel.Workbook xlBook = xlApp.Workbooks.Add(true);
for (columnIndex = 0; columnIndex < columnNum; columnIndex++)
{
xlApp.Cells[1, columnIndex + 1] = dataGridView1.Columns[columnIndex].HeaderText;
}
for (int i = 0; i < rowNum; i++)
{
rowIndex++;
columnIndex = 0;
for (int j = 0; j < columnNum; j++)
{
columnIndex++;
xlApp.Cells[rowIndex + 1, columnIndex] = dataGridView1[j, i].Value + "\t";
}
}
xlBook.SaveCopyAs(strFileName);
MessageBox.Show("OK");