首页 新闻 会员 周边 捐助

winform如何导出Excel

0
[已关闭问题]

winform如何导出Excel

请说详细些,我是新手,呵呵

天气预报的主页 天气预报 | 初学一级 | 园豆:200
提问于:2009-08-05 20:10
< >
分享
其他回答(2)
0

1.应用MS Office和Excel互操作程序集

2.通过Excel应用程序及各级别对象接口对Excel文档进行操作

3.通过Excel文档接口进行保存输出

如果是新手可以通过关键字到网络上找详细的示例

白衣苍狗 | 园豆:250 (菜鸟二级) | 2009-08-06 08:13
0

                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个月.";

masterfy | 园豆:105 (初学一级) | 2009-08-06 09:04
仅供参考!
支持(0) 反对(0) masterfy | 园豆:105 (初学一级) | 2009-08-06 09:06
0

  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;
        }
用这个方法就可以了

江峰 | 园豆:178 (初学一级) | 2009-08-06 13:40
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册