我的任务是将Grid-view数据添加到已经格式化的excel中,我需要将Grid-view数据插入到列的标题/标题之后的现有excel的第一行。在插入数据时我不知道我在哪里做错了?请在下面找到我的模板excel,asp-dot-net代码。
Excel中
Column1 Column2 Total1
=Sum(A2+B2)
=Sum(A2+B2)
Dotnet代码:
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="ExcelExport._Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<h2>
Welcome to ASP.NET!
</h2>
<p>
To learn more about ASP.NET visit <a href="http://www.asp.net" title="ASP.NET Website">www.asp.net</a>.
</p>
<p>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Show" />
<asp:Button ID="Button2" runat="server" Text="Export to Excel"
onclick="Button2_Click" />
</p>
<p>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="Column1" HeaderText="A" />
<asp:BoundField DataField="Column2" HeaderText="B" />
<asp:BoundField DataField="Total" HeaderText="Total" />
<asp:BoundField DataField="Total1" HeaderText="FinalTotal" />
</Columns>
</asp:GridView>
</p>
<p>
You can also find <a href="http://go.microsoft.com/fwlink/?LinkID=152368&clcid=0x409"
title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDN</a>.
</p>
</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.OleDb;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
namespace ExcelExport
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string connection = null;
connection = ConfigurationManager.ConnectionStrings["Local"].ConnectionString;
SqlConnection con = new SqlConnection(connection);
string query = "select * from calc";
con.Open();
SqlCommand cmd = new SqlCommand(query, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
if (ds.Tables.Count > 0)
{
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
protected void Button2_Click(object sender, EventArgs e)
{
int gridi, j, value1, value2, value3;
int rowcount = GridView1.Rows.Count;
int Colcount = 3;
String[] cellText;
string constr, sheetinfo = "";
string path = "D:\\Template.xlsx";
constr = ConfigurationManager.ConnectionStrings["Excel07+ConString"].ConnectionString;
constr = string.Format(constr, path);
using (OleDbConnection excel_con = new OleDbConnection(constr))
{
excel_con.Open();
string sheet1 = excel_con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null).Rows[0]["TABLE_NAME"].ToString();
DataTable dtExcelData = new DataTable();
dtExcelData = excel_con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
String[] excelSheets = new String[dtExcelData.Rows.Count];
int i = 0;
foreach (DataRow row in dtExcelData.Rows)
{
excelSheets[i] = row["TABLE_NAME"].ToString();
i++;
}
// sheetinfo = excelSheets[2];
try
{
foreach (GridViewRow row in GridView1.Rows)
{
int count = 2;
value1 = Convert.ToInt32(row.Cells[0].Text.Trim());
value2 = Convert.ToInt32(row.Cells[1].Text.Trim());
//value3 = Convert.ToInt32(row.Cells[2].Text.Trim());
//string excelquery = "Insert into ["+sheetinfo+"A2:C20000] (Column1,Column2,Column3) values(" + value1 + "," + value2 + "," + value3 + ")";
//string excelquery = "Insert into [sheet1$](A" + count + ",B" + count + ",C" + count + ") values(" + value1 + "," + value2 + "," + value3 + ")";
string excelquery = "Insert into [Sheet1$](Column1, Column2) values(" + value1 + "," + value2 + ")";
using (OleDbDataAdapter oda = new OleDbDataAdapter(excelquery, excel_con))
{
oda.Fill(dtExcelData);
}
excel_con.Close();
count += 1;
}
}
catch (Exception ex)
{
}
}
}
}
}