当前位置:编程学习 > C#/ASP.NET >>

asp.net datagrid 导出excel二种方法

asp教程.net datagrid 导出excel二种方法
文章为你提供二款datagrid 导出excel的代码一款是利用vb.net 导出excel一种是利用c# datagrid 导出excel的实例代码。
*/
//c#:

 

private void exporttoexcel()
        {
            savefiledialog savefiledialog = new savefiledialog();
            savefiledialog.filter = "execl files (*.xls)|*.xls";
            savefiledialog.filterindex = 0;
            savefiledialog.restoredirectory = true;
            savefiledialog.createprompt = true;
            savefiledialog.title = "保存为excel文件";
            savefiledialog.showdialog();

            if (savefiledialog.filename.indexof(":") < 0) return; //被点了"取消"

            stream mystream;
            mystream = savefiledialog.openfile();
            streamwriter sw = new streamwriter(mystream, system.text.encoding.getencoding(-0));
            string columntitle = "";
            try
            {
                //写入列标题
                for (int i = 0; i < dgvgf_in_info.columncount; i++)
                {
                    if (i > 0)
                    {
                        columntitle += "t";
                    }
                    columntitle += dgvgf_in_info.columns[i].headertext;
                }
                sw.writeline(columntitle);

                //写入列内容
                for (int j = 0; j < dgvgf_in_info.rows.count; j++)
                {
                    string columnvalue = "";
                    for (int k = 0; k < dgvgf_in_info.columns.count; k++)
                    {
                        if (k > 0)
                        {
                            columnvalue += "t";
                        }
                        if (dgvgf_in_info.rows[j].cells[k].value == null)
                            columnvalue += "";
                        else
                            columnvalue += dgvgf_in_info.rows[j].cells[k].value.tostring().trim();
                    }
                    sw.writeline(columnvalue);
                }
                sw.close();
                mystream.close();
            }
            catch (exception e)
            {
                messagebox.show(e.tostring());
            }
            finally
            {
                sw.close();
                mystream.close();
            }
        }

//vb.net

private sub exporttoexcel()
   dim savefiledialog as new savefiledialog()
   savefiledialog.filter = "execl files (*.xls)|*.xls"
   savefiledialog.filterindex = 0
   savefiledialog.restoredirectory = true
   savefiledialog.createprompt = true
   savefiledialog.title = "保存为excel文件"
   savefiledialog.showdialog()

   if savefiledialog.filename.indexof(":") < 0 then
    return
   end if
   '被点了"取消"
   dim mystream as stream
   mystream = savefiledialog.openfile()
   dim sw as new streamwriter(mystream, system.text.encoding.getencoding(-0))
   dim columntitle as string = ""
   try
    '写入列标题
    for i as integer = 0 to dgvgf_in_info.columncount - 1
     if i > 0 then
      columntitle += vbtab
     end if
     columntitle += dgvgf_in_info.columns(i).headertext
    next
    sw.writeline(columntitle)

    '写入列内容
    for j as integer = 0 to dgvgf_in_info.rows.count - 1
     dim columnvalue as string = ""
     for k as integer = 0 to dgvgf_in_info.columns.count - 1
      if k > 0 then
       columnvalue += vbtab
      end if
      if dgvgf_in_info.rows(j).cells(k).value is nothing then
       columnvalue += ""
      else
       columnvalue += dgvgf_in_info.rows(j).cells(k).value.tostring().trim()
      end if
     next
     sw.writeline(columnvalue)
    next
    sw.close()
    mystream.close()
   catch e as exception
    messagebox.show(e.tostring())
   finally
    sw.close()
    mystream.close()
   end try
  end sub

 

%>

补充:asp.net教程,.Net开发
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,