winform导出excel

也是复制别人的代码,嘿嘿~ 

private void BtnExport_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable dt = ReportManage.BYTable(TxtBusnum.Text, TxtMentantsort.Text, TxtMentantname.Text, TxtManager.Text, comboBoxstime.Text, comboBoxetime.Text);
                if (dt == null)
                {
                    MessageBox.Show("没有数据可导出");
                    return;
                }
                if (dt.Rows.Count == 0)
                {
                    MessageBox.Show("没有数据可导出");
                    return;
                }
                Excel.Application xlApp = new Excel.Application();
                if (xlApp == null)
                {
                    MessageBox.Show("请确保您的电脑已经安装Excel", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                xlApp.UserControl = true;
                Excel.Workbooks workbooks = xlApp.Workbooks;
                //根据模版产生新的workbook //Workbook workbook = workbooks.Add("D://aa.xls");
                Excel.Workbook workbook = workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
                Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];//取得sheet1
                if (worksheet == null)
                {
                    MessageBox.Show("请确保您的电脑已经安装Excel", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                try
                {
                    Excel.Range range;
                    long totalCount = dt.Rows.Count;
                    long rowRead = 0;
                    float percent = 0;
                    worksheet.Cells[1, 1] = "车牌号";//导出的标题
                    worksheet.Cells[1, 2] = "车辆品牌";
                    worksheet.Cells[1, 3] = "车辆型号";
                    worksheet.Cells[1, 4] = "车辆类别";//导出的标题
                    worksheet.Cells[1, 5] = "保养日期";
                    worksheet.Cells[1, 6] = "保养类别";
                    worksheet.Cells[1, 7] = "金额";
                    worksheet.Cells[1, 8] = "保养里程";
                    worksheet.Cells[1, 9] = "经办人";
                    worksheet.Cells[1, 10] = "保养点";
                    worksheet.Cells[1, 11] = "保养项目";
                    worksheet.Cells[1, 12] = "备注";
                    worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[1, 1]).MergeCells = true; //合并单元格---列数
                    worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[1, 3]).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;//居中对齐
                    worksheet.get_Range(worksheet.Cells[1, 3], worksheet.Cells[1, 3]).ColumnWidth = 15;     //列宽
                    worksheet.get_Range(worksheet.Cells[1, 2], worksheet.Cells[1, 2]).ColumnWidth = 15;     //列宽
                    worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[1, 1]).ColumnWidth = 20;     //列宽

                    for (int r = 0; r < dt.Rows.Count; r++)
                    {
                        for (int i = 0; i < dt.Columns.Count; i++)
                        {
                            worksheet.Cells[r + 3, i + 1] = dt.Rows[r][i];
                        }
                        rowRead++;
                        percent = ((float)(100 * rowRead)) / totalCount;

                        worksheet.get_Range(worksheet.Cells[r + 3, 4], worksheet.Cells[r + 1, 4]).Columns.WrapText = true;     //自动换行
                        worksheet.get_Range(worksheet.Cells[r + 3, 4], worksheet.Cells[r + 3, 4]).Rows.AutoFit(); //自动加行高

                    }

                    range = worksheet.get_Range(worksheet.Cells[2, 1], worksheet.Cells[dt.Rows.Count + 2, dt.Columns.Count]);
                    range.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThin, Excel.XlColorIndex.xlColorIndexAutomatic, null);

                    range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;
                    range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].LineStyle = Excel.XlLineStyle.xlContinuous;
                    range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].Weight = Excel.XlBorderWeight.xlThin;

                    if (dt.Columns.Count > 1)
                    {
                        range.Borders[Excel.XlBordersIndex.xlInsideVertical].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;
                        range.Borders[Excel.XlBordersIndex.xlInsideVertical].LineStyle = Excel.XlLineStyle.xlContinuous;
                        range.Borders[Excel.XlBordersIndex.xlInsideVertical].Weight = Excel.XlBorderWeight.xlThin;
                    }
                    xlApp.Visible = true;

                }
                catch
                {
                    MessageBox.Show("到出Excel失败!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Error);

                }
                finally
                {

                    System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);

                    System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);

                    System.Runtime.InteropServices.Marshal.ReleaseComObject(workbooks);

                    System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
                    //KillProcess("Excel");
                    GC.Collect();//强行销毁         
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("出现异常,异常信息:" + ex.ToString());
            }
        }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值