zoukankan      html  css  js  c++  java
  • DataGridView 导出到Excel

    #region 导出四个表格到Excel
    /// <summary>
    /// 导出四个表格到Excel
    /// </summary>
    /// <param name="dgvPaoDian"></param>
    /// <param name="strPaoDian"></param>
    /// <param name="dgvGJS"></param>
    /// <param name="strGJS"></param>
    /// <param name="dgvSHZDS"></param>
    /// <param name="strSHZDS"></param>
    /// <param name="dgvQYZDS"></param>
    /// <param name="strQYZDS"></param>
    /// <param name="FilePathName"></param>
    /// <returns></returns>
    public static bool MultiGvwToExcel(DataGridViewX dgvPaoDian, string strPaoDian, DataGridViewX dgvGJS, string strGJS, DataGridViewX dgvSHZDS, string strSHZDS, DataGridViewX dgvQYZDS, string strQYZDS, ref string FilePathName)
    {
    bool MyReturnValye = false;
    if (dgvPaoDian.DataSource == null && dgvGJS.DataSource == null && dgvSHZDS.DataSource == null && dgvQYZDS.DataSource == null)
    {
    MyReturnValye = false;
    //没有绑定任何数据源
    }
    else
    {
    string path;
    SaveFileDialog savefile = new SaveFileDialog();
    savefile.Filter = "Execl files(*.xls)|*.xls";
    if (savefile.ShowDialog() == DialogResult.OK && savefile.FileName != "")
    {
    //获取保存路径
    path = savefile.FileName;
    Workbook wb = new Workbook();
    wb.Worksheets.Add();
    wb.Worksheets.Add();
    wb.Worksheets.Add();
    Worksheet ws = wb.Worksheets[0];
    ws.Name = strPaoDian;
    Worksheet ws1 = wb.Worksheets[1];
    ws1.Name = strGJS;
    Worksheet ws2 = wb.Worksheets[2];
    ws2.Name = strSHZDS;
    Worksheet ws3 = wb.Worksheets[3];
    ws3.Name = strQYZDS;
    Cells cell;
    int i, j;

    //设置风格

    #region 第一个sheet
    if (dgvPaoDian.DataSource != null)
    {
    if (dgvPaoDian.RowCount > 0)
    {
    cell = ws.Cells;
    Style style1 = wb.Styles[wb.Styles.Add()];
    style1.Font.Name = "宋体";
    style1.Font.IsBold = false;//设置粗体
    style1.Font.Size = 12;//设置字体大小
    style1.HorizontalAlignment = TextAlignmentType.Center;

    //设置标题
    for (i = 0; i < dgvPaoDian.ColumnCount; i++)
    {
    cell[0, i].PutValue(dgvPaoDian.Columns[i].HeaderText.ToString());
    cell[0, i].SetStyle(style1);
    }
    //设置内容
    for (i = 0; i < dgvPaoDian.RowCount; i++)
    {
    for (j = 0; j < dgvPaoDian.ColumnCount; j++)
    {
    cell[i + 1, j].PutValue(dgvPaoDian.Rows[i].Cells[j].Value.ToString());
    cell[i + 1, j].SetStyle(style1);
    }
    }

    //设置列宽
    for (i = 0; i < dgvPaoDian.ColumnCount; i++)
    {
    cell.SetColumnWidth(i, 14);
    }
    }
    cell = null;
    }
    #endregion

    #region 第二个sheet
    if (dgvGJS.DataSource != null)
    {
    if (dgvGJS.RowCount > 0)
    {
    cell = ws1.Cells;
    Style style1 = wb.Styles[wb.Styles.Add()];
    style1.Font.Name = "宋体";
    style1.Font.IsBold = false;//设置粗体
    style1.Font.Size = 12;//设置字体大小
    style1.HorizontalAlignment = TextAlignmentType.Center;

    //设置标题
    for (i = 0; i < dgvGJS.ColumnCount; i++)
    {
    cell[0, i].PutValue(dgvGJS.Columns[i].HeaderText.ToString());
    cell[0, i].SetStyle(style1);
    }
    //设置内容
    for (i = 0; i < dgvGJS.RowCount; i++)
    {
    for (j = 0; j < dgvGJS.ColumnCount; j++)
    {
    cell[i + 1, j].PutValue(dgvGJS.Rows[i].Cells[j].Value.ToString());
    cell[i + 1, j].SetStyle(style1);
    }
    }
    //设置列宽
    for (i = 0; i < dgvGJS.ColumnCount; i++)
    {
    cell.SetColumnWidth(i, 14);
    }
    }
    cell = null;
    }
    #endregion

    #region 第三个sheet
    if (dgvSHZDS.DataSource != null)
    {
    if (dgvSHZDS.RowCount > 0)
    {
    cell = ws2.Cells;
    Style style1 = wb.Styles[wb.Styles.Add()];
    style1.Font.Name = "宋体";
    style1.Font.IsBold = false;//设置粗体
    style1.Font.Size = 12;//设置字体大小
    style1.HorizontalAlignment = TextAlignmentType.Center;

    //设置标题
    for (i = 0; i < dgvSHZDS.ColumnCount; i++)
    {
    cell[0, i].PutValue(dgvSHZDS.Columns[i].HeaderText.ToString());
    cell[0, i].SetStyle(style1);
    }
    //设置内容
    for (i = 0; i < dgvSHZDS.RowCount; i++)
    {
    for (j = 0; j < dgvSHZDS.ColumnCount; j++)
    {
    cell[i + 1, j].PutValue(dgvSHZDS.Rows[i].Cells[j].Value.ToString());
    cell[i + 1, j].SetStyle(style1);
    }
    }
    //设置列宽
    for (i = 0; i < dgvSHZDS.ColumnCount; i++)
    {
    cell.SetColumnWidth(i, 14);
    }
    }
    cell = null;
    }
    #endregion

    #region 第四个sheet
    if (dgvQYZDS.DataSource != null)
    {
    if (dgvQYZDS.RowCount > 0)
    {
    cell = ws3.Cells;
    Style style1 = wb.Styles[wb.Styles.Add()];
    style1.Font.Name = "宋体";
    style1.Font.IsBold = false;//设置粗体
    style1.Font.Size = 12;//设置字体大小
    style1.HorizontalAlignment = TextAlignmentType.Center;

    //设置标题
    for (i = 0; i < dgvQYZDS.ColumnCount; i++)
    {
    cell[0, i].PutValue(dgvQYZDS.Columns[i].HeaderText.ToString());
    cell[0, i].SetStyle(style1);
    }
    //设置内容
    for (i = 0; i < dgvQYZDS.RowCount; i++)
    {
    for (j = 0; j < dgvQYZDS.ColumnCount; j++)
    {
    cell[i + 1, j].PutValue(dgvQYZDS.Rows[i].Cells[j].Value.ToString());
    cell[i + 1, j].SetStyle(style1);
    }
    }
    //设置列宽
    for (i = 0; i < dgvQYZDS.ColumnCount; i++)
    {
    cell.SetColumnWidth(i, 14);
    }
    }
    cell = null;
    }
    #endregion

    wb.Save(path);
    FilePathName = path;
    MyReturnValye = true;
    }
    else
    {
    MyReturnValye = false;
    }
    }
    return MyReturnValye;
    }
    #endregion

  • 相关阅读:
    《Java TCP/IP Socket 编程 》读书笔记之十一:深入剖析socket——TCP套接字的生命周期
    c++实现二分查找
    hadoop序列化机制与java序列化机制对比
    C、C++中“*”操作符和“后++”操作符的优先级
    poj2774之最长公共子串
    Python之美[从菜鸟到高手]--urlparse源码分析
    (程序员面试题)字符串处理之寻找最大不重复子串
    hdu 4782 Beautiful Soupz
    教程Xcode 下编译发布与提交App到AppStore
    云端的ABAP Restful服务开发
  • 原文地址:https://www.cnblogs.com/yuxuetaoxp/p/4649219.html
Copyright © 2011-2022 走看看