1.应用MS Office和Excel互操作程序集
2.通过Excel应用程序及各级别对象接口对Excel文档进行操作
3.通过Excel文档接口进行保存输出
如果是新手可以通过关键字到网络上找详细的示例
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbooks workbooks = excel.Workbooks;
Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
Microsoft.Office.Interop.Excel.Sheets worksheets = workbook.Worksheets;
Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)worksheets.get_Item(1);
excel.Visible = true;
Microsoft.Office.Interop.Excel.Range range;
excel.Cells.Select();
excel.Cells.RowHeight = 15;
excel.Cells.Font.Size = 9;
excel.Cells.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
int rowPos = 1, coluPos = 0;
range = excel.get_Range(sheet.Cells[rowPos, 1], sheet.Cells[rowPos, 9]);
range.Select();
range.Merge(true);
range.Font.Name = "宋体";
range.Font.Size = 10;
range.Font.Bold = true;
sheet.Cells[rowPos, 1] = bjjhInfo.JHSM;
rowPos++;
GlOp.ExportToExcelFromXtraGrid(this.gridView2, excel, sheet, ref range, ref rowPos, ref coluPos);
range = excel.get_Range(sheet.Cells[rowPos, 1], sheet.Cells[rowPos, 9]);
range.Merge(true);
range.Font.Name = "宋体";
range.Font.Size = 9;
range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;
sheet.Cells[rowPos, 1] = "填报人:" + bjjhInfo.TBRXM + " " + bjjhInfo.TBSJ + " 填报单位负责人:" + bjjhInfo.TBDWFZRXM + " 设备科签收人:" + bjjhInfo.SBKQSRXM + " " + bjjhInfo.SBKQSSJ;
rowPos++;
range = excel.get_Range(sheet.Cells[rowPos, 1], sheet.Cells[rowPos, 9]);
range.Merge(true);
range.Font.Name = "宋体";
range.Font.Size = 9;
range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;
sheet.Cells[rowPos, 1] = "注:1.本表一式2份,每月15日前报设备科备品组,当月25日设备科备品组反馈1份给填报单位";
rowPos++;
range = excel.get_Range(sheet.Cells[rowPos, 1], sheet.Cells[rowPos, 9]);
range.Merge(true);
range.Font.Name = "宋体";
range.Font.Size = 9;
range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;
sheet.Cells[rowPos, 1] = " 2.一般交货周期:非标备件2-6个月,通用标准备件1-3个月,进口备件6-12个月.";
public bool ExportDataGridview(DataGridView gridView, bool isShowExcle)
{
if (gridView.Rows.Count == 0)
return false;
//建立Excel对象
Excel.Application excel = new Excel.Application();
excel.Application.Workbooks.Add(true);
excel.Visible = isShowExcle;
//生成字段名称
for (int i = 0; i < gridView.ColumnCount; i++)
{
excel.Cells[1, i + 1] = gridView.Columns[i].HeaderText;
}
//填充数据
for (int i = 0; i < gridView.RowCount; i++)
{
for (int j = 0; j < gridView.ColumnCount; j++)
{
if (gridView[j, i].ValueType == typeof(string))
{
excel.Cells[i + 2, j + 1] = "'" + gridView[j, i].Value.ToString();
}
else
{
excel.Cells[i + 2, j + 1] = gridView[j, i].Value.ToString();
}
}
}
return true;
}
用这个方法就可以了