使用combobox导出/导入到Excel

我仍然在苦苦挣扎。 我到处寻找解决scheme并找不到解决scheme。 我正在使用C#和ASP.NET,我需要将数据导出到Excel电子表格。

我有一个包含Application对象的应用程序列表。 每个应用程序对象都有一个状态ID。 当我将这些应用程序导出到Excel时,我需要一个带有下拉/combobox的列。 在这个下拉菜单/combobox中应该是数据库中所有状态的列表。 然后我需要将状态select为应用程序的状态ID。

另一个问题是关于上述情况。 当用户下载Excel文档时,用户可以通过在下拉列表中select不同的值来更改每个应用程序的状态。 我将需要上传这个电子表格并循环遍历行,并在下拉/combobox中获取列的值和所选项目的值。 你会怎么做? 这可能吗?

我可能不会使用COM组件,所以我尝试使用EPPlus或NPOI(它不一定是这两个),但似乎并不支持我的场景。 我将如何能够实现我的scheme? 再一次,它不必使用EPPlus和NPOI。 工作示例将不胜感激:)

谢谢

布伦丹

你有没有尝试在NPOI中使用模板? 当我想用NPOI导出复杂的数据表时,我创build一个XLS文档作为模板,并通过C#填充数据。

根本不需要macros。 C#本身会为你做。 在这里,我有一个从excel导入(不pipe是2003/2007)的演示到gridview和导出gridview(EDITED / changed)数据到一个新的EXCEL文件。 尝试一下。 工程100%

您需要做的是:在您的Web应用程序内创build一个名为“Files”的文件夹,并将您的Excel文件保存在“Files”文件夹中。 如果你的数据源(从你的网格填充的地方)不是excel,那么不需要上面的文件夹。你可以在btnImport_Click()里写你自己的代码。 这是你的.aspx页面:

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="ImportFromExcelToDatasetAndGridviewAndExport.aspx.cs" Inherits="ImportFromExcelToGridViewWebApp.ImportFromExcelToDatasetAndGridviewAndExport" %> <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <div> <b>Please Select Excel File : </b> <asp:FileUpload ID="fileuploadExcel" runat="server" />&nbsp;&nbsp; <asp:Button ID="btnImport" runat="server" Text="Import From Excel" OnClick="btnImport_Click" /> <br /> <asp:Label ID="lblMessage" runat="server" Visible="False" Font-Bold="True" ForeColor="#009933"></asp:Label><br /> <asp:GridView ID="grvExcelData" runat="server" AutoGenerateColumns="false" onrowdatabound="GridView2_RowDataBound"> <HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" /> <Columns> <asp:TemplateField ItemStyle-Width = "150px" HeaderText = "UserName"> <ItemTemplate> <asp:TextBox ID="txtUserName" runat="server" CssClass="css2" Text='<%# Eval("UserName")%>'></asp:TextBox> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txtUserName" runat="server" CssClass="css2" Text='<%# Eval("UserName")%>'> </asp:TextBox> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField ItemStyle-Width = "150px" HeaderText = "Education"> <ItemTemplate> <asp:TextBox ID="txtEducation" runat="server" CssClass="css2" Text='<%# Eval("Education")%>'></asp:TextBox> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txtEducation" runat="server" CssClass="css2" Text='<%# Eval("Education")%>'> </asp:TextBox> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField ItemStyle-Width = "150px" HeaderText = "Location"> <ItemTemplate> <asp:TextBox ID="txtLocation" runat="server" CssClass="css2" Text='<%# Eval("Location")%>'></asp:TextBox> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txtLocation" runat="server" CssClass="css2" Text='<%# Eval("Location")%>'> </asp:TextBox> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Select"> <ItemTemplate> <asp:DropDownList ID="DrdDatabase" Width="100px" runat="server"> </asp:DropDownList> </ItemTemplate> <EditItemTemplate> <asp:DropDownList ID="DrdDatabase" Width="100px" runat="server"> </asp:DropDownList> </EditItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <asp:Button ID="btnExport" runat="server" Text="Export To Excel" OnClick="btnExport_Click" /> </div> </asp:Content> 

这里是你的代码。

  using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.IO; using System.Data.OleDb; using System.Collections; namespace ImportFromExcelToGridViewWebApp { public partial class ImportFromExcelToDatasetAndGridviewAndExport : System.Web.UI.Page { private ArrayList ExcelData { get { object excel = Session["dropdownvalue"]; if (excel == null) Session["dropdownvalue"] = new ArrayList(); return (ArrayList)Session["dropdownvalue"]; } set { Session["dropdownvalue"] = value; } } protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { DropDownList ddl = (DropDownList)e.Row.FindControl("DrdDatabase"); foreach (string colName in ExcelData) ddl.Items.Add(new ListItem(colName)); } } protected void btnImport_Click(object sender, EventArgs e) { ArrayList alist = new ArrayList(); string connString = ""; string strFileType = Path.GetExtension(fileuploadExcel.FileName).ToLower(); string fileBasePath = Server.MapPath("~/Files/"); string fileName = Path.GetFileName(this.fileuploadExcel.FileName); string fullFilePath = fileBasePath + fileName; //Connection String to Excel Workbook if (strFileType.Trim() == ".xls") { connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fullFilePath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\""; } else if (strFileType.Trim() == ".xlsx") { connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fullFilePath + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=1\""; } if (fileuploadExcel.HasFile) { string query = "SELECT [UserName],[Education],[Location] FROM [Sheet1$]"; OleDbConnection conn = new OleDbConnection(connString); if (conn.State == ConnectionState.Closed) conn.Open(); OleDbCommand cmd = new OleDbCommand(query, conn); OleDbDataAdapter da = new OleDbDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); //Assigning dataset table data to GridView Control Session["griddata"] = ds.Tables[0]; System.Data.DataTable dtt = (System.Data.DataTable)Session["griddata"]; //griddata is the gridview data from another page var res = (from f in dtt.AsEnumerable() select f.Field<string>("Location") ); foreach (string s in res.AsEnumerable()) { alist.Add(s); } Session["dropdownvalue"] = alist; grvExcelData.DataSource = Session["griddata"]; grvExcelData.DataBind(); da.Dispose(); conn.Close(); conn.Dispose(); } } protected void btnExport_Click(object sender, EventArgs e) { System.Data.DataTable dtExcel = new DataTable(); dtExcel.Columns.Add("UserName", typeof(string)); dtExcel.Columns.Add("Education", typeof(string)); dtExcel.Columns.Add("Location", typeof(string)); dtExcel.Columns.Add("Select", typeof(string)); foreach (GridViewRow row in grvExcelData.Rows) { string UserName = ((TextBox)row.FindControl("txtUserName")).Text; string Education = ((TextBox)row.FindControl("txtEducation")).Text; string Location = ((TextBox)row.FindControl("txtLocation")).Text; string Location2 = ((DropDownList)row.FindControl("DrdDatabase")).Text; DataRow dr = dtExcel.NewRow(); dr["UserName"] = UserName; dr["Education"] = Education; dr["Location"] = Location; dr["Select"] = Location2; dtExcel.Rows.Add(dr); } CreateWorkbook(dtExcel, Server.MapPath("~/DownlodedFiles/Excel.xls")); } public static void CreateWorkbook(System.Data.DataTable dtExcelData, String path) { int rowindex = 0; int columnindex = 1; Microsoft.Office.Interop.Excel.Application app; Microsoft.Office.Interop.Excel.Worksheet wksheet; Microsoft.Office.Interop.Excel.Workbook wkbook; app = new Microsoft.Office.Interop.Excel.Application(); app.Visible = false; wkbook = app.Workbooks.Add(true); wksheet = (Microsoft.Office.Interop.Excel.Worksheet)wkbook.ActiveSheet; try { for (int i = 0; i < dtExcelData.Columns.Count; i++) { wksheet.Cells[1, i + 1] = dtExcelData.Columns[i].ColumnName; } foreach (DataRow row in dtExcelData.Rows) { rowindex++; columnindex = 0; foreach (DataColumn col in dtExcelData.Columns) { columnindex++; wksheet.Cells[rowindex + 1, columnindex] = row[col.ColumnName]; } } app.Visible = true; } catch (Exception ex1) { } app.UserControl = true; } } }