//菜单事件
private void ExportTestPlanToolStripMenuItem_Click(object sender, EventArgs e)
{
dt = tempPlan.GetTestPlanInfo();
InputAndOpenExcel(dt);
}
private void InputAndOpenExcel(DataTable dt)
{
Excel.Application excel = new Excel.Application();
excel.Workbooks.Add(true);
excel.Cells.ColumnWidth = 16;
excel.Cells.Font.Name = "楷体";
excel.Cells.Font.Size = 11;
int row = 2;
int span = 0 ; // 用来跳过不希望输出的字段
//把数据表的各个信息输入到excel表中
//取字段名
for (int i = 0; i < dt.Columns.Count; i++)
{
剔出dataTable中的0、5、7列
if (i == 0 || i == 5 || i == 7)
{
span ++;
continue;
}
excel.Cells[1, i - span + 1] = dt.Columns[i].ColumnName.ToString();
}
//取记录值
span = 0;
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
if (j ==0 || j == 5 || j == 7)
{
span ++;
continue;
}
excel.Cells[row, j - span + 1] = dt.Rows[i][j].ToString();
}
span = 0;
row++;
}
//使excel可见,即打开 */
excel.Visible = true;
}