C#中如何将数据导入到Word和Excel中5
发布网友
发布时间:2023-11-10 03:26
我来回答
共1个回答
热心网友
时间:2024-02-15 15:08
//建立Excel对象
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
excel.Application.Workbooks.Add(true);
excel.Visible = true;
//生成字段名称
excel.get_Range(excel.Cells[1, 1], excel.Cells[1, 7]).Merge(Type.Missing);
excel.Cells[1, 1] = this.txtTableChineseName.Text + "(" + this.txtTableName.Text + ")";
excel.get_Range(excel.Cells[1, 1], excel.Cells[1, 7]).Cells.Borders.LineStyle = 1;
excel.get_Range(excel.Cells[1, 1], excel.Cells[1, 1]).Font.ColorIndex = 41;
excel.get_Range(excel.Cells[1, 1], excel.Cells[1, 1]).Interior.ColorIndex = 35;
excel.get_Range(excel.Cells[1, 1], excel.Cells[1, 1]).Font.Size = 12;
excel.get_Range(excel.Cells[1, 1], excel.Cells[1, 1]).Font.Bold = true;
excel.Cells[2, 1] = "字段中文名";
excel.Cells[2, 2] = "字段英文名";
excel.Cells[2, 3] = "数据类型";
excel.Cells[2, 4] = "是否主键";
excel.Cells[2, 5] = "可否空";
excel.Cells[2, 6] = "默认值";
excel.Cells[2, 7] = "备注";
excel.get_Range(excel.Cells[2, 1], excel.Cells[2, 7]).Interior.ColorIndex = 6;
excel.get_Range(excel.Cells[2, 1], excel.Cells[2, 7]).HorizontalAlignment = Microsoft.Office.Interop.Excel.Constants.xlCenter;
excel.get_Range(excel.Cells[2, 1], excel.Cells[2, 7]).Font.Size = 9;
excel.get_Range(excel.Cells[2, 1], excel.Cells[2, 7]).Cells.Borders.LineStyle = 1;
excel.get_Range(excel.Cells[2, 1], excel.Cells[2, 7]).Font.Bold = true;
//填充数据
for (int i = 0; i < lstTableFiledList.Items.Count; i++)
{
for (int j = 0; j < 6; j++)
{
ListViewItem item = this.lstTableFiledList.Items[i];
excel.Cells[i + 3, 1] = "'"+item.SubItems[1].Text;
excel.Cells[i + 3, 2] = "'" + item.SubItems[0].Text;
if (item.SubItems[3].Text == "")
{
excel.Cells[i + 3, 3] = "'" + item.SubItems[2].Text;
}
else
{
excel.Cells[i + 3, 3] = "'" + item.SubItems[2].Text + "(" + item.SubItems[3].Text + ")";
}
excel.Cells[i + 3, 4] = item.SubItems[4].Text.Trim() == "Yes" ? "True" : "False";
excel.Cells[i + 3, 5] = item.SubItems[5].Text.Trim() == "Null" ? "True" : "False";
excel.Cells[i + 3, 6] = "'" + item.SubItems[6].Text.Trim();
excel.Cells[i + 3, 7] = "'" + item.SubItems[7].Text.Trim();
excel.get_Range(excel.Cells[i + 3, 4], excel.Cells[i + 3, 5]).HorizontalAlignment = Microsoft.Office.Interop.Excel.Constants.xlCenter;
excel.get_Range(excel.Cells[i + 3, 1], excel.Cells[i + 3, 7]).Font.Size = 9;
excel.get_Range(excel.Cells[i + 3, 1], excel.Cells[i + 3, 7]).Cells.Borders.LineStyle = 1;
}
}
}
==================================
Microsoft.Office.Interop.Excel