导出到Excel,导出模板应与导入文件模板相同 [英] Export to Excel and the export template should be same as import file template

查看:105
本文介绍了导出到Excel,导出模板应与导入文件模板相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,



我有一个gridview,可以将gridview数据导出到excel文件中。现在我的要求是,我需要将数据导出到excel中,文件格式应该与导入文件相同,以便导出的文件可以再次用于导入。



我可以使用导出源代码和渲染控件导出Gridview中显示的内容。但是,导入文件与Gridview格式不同(列不同。)



如果有人可以帮助或建议需要为此做什么,我将不胜感激要求。





关于

Dear All,

I have a gridview and can export the gridview data into the excel file. Now my requirement is, I need to export the data into excel and the file format should be same as import file so that the exported file can be used for importing again.

I could export whatever was displayed in Gridview using the export source code and render control. But, import file is different from the Gridview format (columns are different.)

I would greatly appreciate if someone can help or suggest on what needs to be done for this requirement.


Regards

推荐答案

我正在使用以下vb从asp.net导出到excel



I am using the following vb to export to excel from asp.net

Private Sub ExportToExcel(table As DataTable, name As String)
       HttpContext.Current.Response.Clear()
       HttpContext.Current.Response.ClearContent()
       HttpContext.Current.Response.ClearHeaders()
       HttpContext.Current.Response.Buffer = True
       HttpContext.Current.Response.ContentType = "application/ms-excel"
       HttpContext.Current.Response.Write("<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">")
       HttpContext.Current.Response.AddHeader("Content-Disposition", "filename=" + name + ".xls")

       HttpContext.Current.Response.Charset = "utf-8"
       HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1250")
       HttpContext.Current.Response.Write("<font style='font-size:10.0pt; font-family:Calibri;'>")
       HttpContext.Current.Response.Write("<BR><BR><BR>")
       'sets the table border, cell spacing, border color, font of the text, background, foreground, font height
       HttpContext.Current.Response.Write("<Table border='1' bgColor='#ffffff' " + "borderColor='#000000' cellSpacing='0' cellPadding='0' " + "style='font-size:10.0pt; font-family:Calibri; background:white;'> <TR>")
       'Column Headers
       Dim columnscount As Integer = table.Columns.Count

       For j As Integer = 0 To columnscount - 1
           'write column
           HttpContext.Current.Response.Write("<Td>")
           HttpContext.Current.Response.Write(table.Columns(j).ColumnName.ToString())
           HttpContext.Current.Response.Write("</Td>")

       Next
       HttpContext.Current.Response.Write("</TR>")
       For Each row As DataRow In table.Rows
           'write data
           HttpContext.Current.Response.Write("<TR>")
           For i As Integer = 0 To table.Columns.Count - 1
               HttpContext.Current.Response.Write("<Td>")
               HttpContext.Current.Response.Write(row(i).ToString())
               HttpContext.Current.Response.Write("</Td>")
           Next
           HttpContext.Current.Response.Write("</TR>")
       Next
       HttpContext.Current.Response.Write("</Table>")
       HttpContext.Current.Response.Write("</font>")
       HttpContext.Current.Response.Flush()
       HttpContext.Current.Response.[End]()
   End Sub





在此代码中,数据表(表)包含要导出到excel的数据,name是文件名。 excel文件。



对于使用此代码的问题,我建议从数据库中运行一个select来填充数据表所需的i输出的信息,然后运行它通过这段代码。

而不是直接从数据网格导出。



否则我会建议添加隐藏数据网格的列,如果无法运行存储过程以获取所需的导出数据,则使用这些列进行导出



In this code the datatable(table) contains the data you wish to export to excel and name being the file name of the excel file.

For your question using this code I would suggest running a select from the database to populate a Datatable with the desired information for the output and then run it through this code.
rather than exporting directly from the datagrid.

otherwise I would suggest adding hidden columns to the datagrid and using those to export if you are not able to run a stored procedure to get the desired export data


这篇关于导出到Excel,导出模板应与导入文件模板相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆