zoukankan      html  css  js  c++  java
  • 博客开通第二十六天

     

    将DataGridView中的数据导出为Excel文件,方法一

    public static void ExportToExcel(DataGridView dataGridView)//另存新档按钮导出成Excel
    {
    SaveFileDialog saveFileDialog = new SaveFileDialog();
    saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
    saveFileDialog.FilterIndex = 0;
    saveFileDialog.RestoreDirectory = true;
    saveFileDialog.Title = "导?出?Excel";
    saveFileDialog.ShowDialog();
    if (""==saveFileDialog.FileName || null ==saveFileDialog.FileName )
    return;
    Stream myStream;
    myStream = saveFileDialog.OpenFile();
    StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
    string str = "";
    try
    {
    //标括?题琣
    for (int i = 0; i < dataGridView.ColumnCount; i++)
    {
    if (i > 0)
    {
    str += "\t";
    }
    str += dataGridView.Columns[i].HeaderText;
    }
    sw.WriteLine(str);
    //内ú容╕
    for (int j = 0; j < dataGridView.Rows.Count; j++)
    {
    string tempStr = "";
    for (int k = 0; k < dataGridView.Columns.Count; k++)
    {
    if (k > 0)
    {
    tempStr += "\t";
    }
    tempStr += dataGridView.Rows[j].Cells[k].Value.ToString();
    }
    sw.WriteLine(tempStr);
    }
    sw.Close();
    myStream.Close();
    }
    catch (Exception)
    {
    //MessageBox.Show(e.ToString());
    }
    finally

    {
    sw.Close();
    myStream.Close();
    }

    将DataGridView中的数据导出为Excel文件,方法二:

    public void printAll(DataTable dt, DataGridView dg)
    {
    //导出到execl
    try
    {
    //没有数据的话就不往下执行
    if (dt.Rows.Count == 0)
    return;
    //实例化一个Excel.Application对象
    Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();

    //新增加一个工作簿,Workbook是直接保存,不会弹出保存对话框,加上Application会弹出保存对话框,值为false会报错
    excel.Application.Workbooks.Add(true);
    //让后台执行设置为不可见,为true的话会看到打开一个Excel,然后数据在往里写
    excel.Visible = false;
    //生成Excel中列头名称
    for (int i = 0; i < dt.Columns.Count; i++)
    {
    if (dg.Columns[i].Visible == true)
    excel.Cells[1, i + 1] = dg.Columns[i].HeaderText;//输出DataGridView列头名
    else
    {
    dt.Columns.RemoveAt(i);
    i--;
    }
    }

    //把DataGridView当前页的数据保存在Excel中
    if (dt.Rows.Count > 0)
    {
    for (int i = 0; i < dt.Rows.Count; i++)//控制Excel中行,上下的距离,就是可以到Excel最下的行数,比数据长了报错,比数据短了会显示不完
    {
    for (int j = 0; j < dt.Columns.Count; j++)//控制Excel中列,左右的距离,就是可以到Excel最右的列数,比数据长了报错,比数据短了会显示不完
    {
    string str = dt.Rows[i][j].ToString();
    excel.Cells[i + 2, j + 1] = "'" + str;//i控制行,从Excel中第2行开始输出第一行数据,j控制列,从Excel中第1列输出第1列数据,"'" +是以string形式保存,所以遇到数字不会转成16进制
    }
    }
    }
    //设置禁止弹出保存和覆盖的询问提示框
    excel.DisplayAlerts = false;
    excel.AlertBeforeOverwriting = false;

    //保存工作簿,值为false会报错
    // excel.Application.Workbooks.Add(true).Save();
    //保存excel文件
    excel.Save("D:" + "\\KKHMD.xls");

    //确保Excel进程关闭
    excel.Quit();
    excel = null;
    MessageBox.Show("导出成功!");
    System.IO.File.Delete("D:" + "\\KKHMD.xls");

    }
    catch (Exception ex)
    {
    if (ex.Message.Trim().Equals("异常来自 HRESULT:0x800A03EC"))
    {
    }
    else
    {
    MessageBox.Show(ex.Message);
    }
    }

  • 相关阅读:
    HBase with MapReduce (MultiTable Read)
    HBase with MapReduce (SummaryToFile)
    HBase with MapReduce (Summary)
    HBase with MapReduce (Read and Write)
    HBase with MapReduce (Only Read)
    Hbase中的BloomFilter(布隆过滤器)
    HBase的快照技术
    How To Use Hbase Bulk Loading
    Cloudera-Manager修改集群的IP
    Java中的HashSet和TreeSet
  • 原文地址:https://www.cnblogs.com/licc09/p/3028823.html
Copyright © 2011-2022 走看看