• 导出DataGridView数据到Excel表


    两个办法:

    第一种针对所有的想都是汉字数字,日期和超过string长度的项不能正常显示

    //选择创建文件的路径
    SaveFileDialog save = new SaveFileDialog();
    save.Filter = "excel files(*.xls)|*.xls";
    save.Title = "请选择要导出数据的位置";
    save.FileName = name+DateTime.Now.ToLongDateString();
    if (save.ShowDialog() == DialogResult.OK)
    {
    string fileName=save.FileName;
    //MessageBox.Show(save.FileName);
    // 创建Excel对象
    Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
    if (excel == null)
    {
    MessageBox.Show("Excel无法启动", "提示");
    return;
    }
    //创建Excel工作薄
    Microsoft.Office.Interop.Excel.Workbook excelBook = excel.Workbooks.Add(true);
    Microsoft.Office.Interop.Excel.Worksheet excelSheet = (Microsoft.Office.Interop.Excel.Worksheet)excelBook.Worksheets[1];
    //excel.Application.Workbooks.Add(true);
    //生成字段名称
    int k = 0;
    for (int i = 0; i < dgv.ColumnCount; i++)
    {
    if (dgv.Columns[i].Visible) //不导出隐藏的列
    {
    excel.Cells[1, k + 1] = dgv.Columns[i].HeaderText;
    k++;
    }
    }
    //填充数据
    for (int i = 0; i < dgv.RowCount; i++)
    {
    k = 0;
    for (int j = 0; j < dgv.ColumnCount; j++)
    {
    if (dgv.Columns[j].Visible) //不导出隐藏的列
    {
    if (dgv[j, i].ValueType == typeof(string))
    {
    excel.Cells[i + 2, k + 1] = "" + dgv[j, i].Value.ToString();
    }
    else
    {
    excel.Cells[i + 2, k + 1] = dgv[j, i].Value.ToString();
    }
    }
    k++;
    }
    }
    try
    {
    excelBook.Saved = true;
    excelBook.SaveCopyAs(fileName);
    }
    catch
    {
    MessageBox.Show("导出失败,文件可能正在使用中","提示");
    }

    第二种适合所有的数据

    try
    {
    if (dataGridView.RowCount <= 0)
    {
    MessageBox.Show("没有数据可以保存", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
    return;
    }
    string saveFileName = string.Empty;
    SaveFileDialog saveDialog = new SaveFileDialog();
    saveDialog.DefaultExt = "xls";
    saveDialog.Filter = "Excel文件|*.xls";
    saveDialog.FileName = strFileName;
    saveDialog.ShowDialog();
    saveFileName = saveDialog.FileName;
    if (saveFileName.IndexOf(":") < 0) return;
    Excel.Application myExcelApp = new Excel.Application();
    if (myExcelApp == null)
    {
    MessageBox.Show("无法创建Excel对象,可能您的机子未安装Excel", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
    return;
    }
    Excel.Workbooks workbooks = myExcelApp.Workbooks;
    Excel.Workbook workbook = workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
    Excel.Sheets sheets = workbook.Worksheets;
    Excel.Worksheet worksheet = (Excel.Worksheet)sheets.get_Item(1);
    Excel.Range range;
    object oMis = System.Reflection.Missing.Value;
    //显示为文本格式
    range = worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[dataGridView.RowCount + 1, dataGridView.ColumnCount]);
    range.NumberFormatLocal = "@";
    //读入数据
    //标题行
    for (int i = 0; i < dataGridView.ColumnCount; i++)
    {
    if (dataGridView.Columns[i].Visible)
    {
    worksheet.Cells[1, i + 1] = dataGridView.Columns[i].HeaderText.ToString().Trim();
    }
    }
    for (int r = 0; r < dataGridView.RowCount; r++)
    {
    for (int i = 0; i < dataGridView.ColumnCount; i++)
    {
    if (dataGridView.Columns[i].Visible)
    {
    worksheet.Cells[r + 2, i + 1] = dataGridView.Rows[r].Cells[i].Value.ToString().Trim();
    }
    }
    }
    range = worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[dataGridView.RowCount + 1, dataGridView.ColumnCount]);
    range.Columns.AutoFit();
    range.RowHeight = 18;
    range.HorizontalAlignment = Excel.XlHAlign.xlHAlignLeft;
    #region 设置表格边框样式
    //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 (dataGridView.ColumnCount > 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;
    //}
    #endregion
    //保存
    if (saveFileName != string.Empty)
    {
    try
    {
    workbook.Saved = true;
    workbook.SaveCopyAs(saveFileName);
    }
    catch (Exception ex)
    {
    MessageBox.Show("导出文件时出错,文件可能正被打开! " + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    }
    else
    {
    MessageBox.Show("文件名不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    myExcelApp.Visible = false;
    myExcelApp.Quit();
    GC.Collect();//强行销毁
    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    }

  • 相关阅读:
    Java常见问题汇总
    前端url参数中带有callback并产生错误
    shiro中ecache-core版本引起的异常
    深入SpringMVC注解
    导出表格数据到excel并下载(HSSFWorkbook版)
    layui数据表格及分页
    签名的生成
    程序的健壮性Robustness
    ASP.NET MVC中注册Global.asax的Application_Error事件处理全局异常
    生成二维码功能
  • 原文地址:https://www.cnblogs.com/1990wz/p/4278409.html
Copyright © 2020-2023  润新知