Ich erhalte einen Fehler beim Speichern von Daten in Excel aus der Datenbank mit C#. Wenn ich in den Button klicke, erhalte ich nur eine Datei in meiner Excel-Datei welcome.xlsx Bitte schlagen Sie mir vor, diesen Fehler zu beheben. danke System.Runtime.InteropServices.COMException (0x800A03EC): Ausnahme von HRESULT: 0x800A03EC bei Microsoft.Office.Interop.Excel._Worksheet.set_Name (String RHS) bei BasicGridView.btnclick (Object sender, EventArgs e) in F: \ shubham \ esin \ esin \ Default.aspx.cs: Linie 73Exportieren von Daten aus der Datenbank zu Excel in asp.net
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Configuration;
using System.Data.SqlClient;
using System.Data;
public partial class BasicGridView : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string connectionString = "server=Myserver; database=northwind; user id=sasa; password=123;integrated security=false ";
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("SELECT * FROM uregistration", con);
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
DataTable dt = new DataTable();
dt.TableName = "uregistration";
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void btnclick(object sender, EventArgs e)
{
string connectionString = "server=Myserver; database=northwind; user id=sa; password=123;integrated security=false ";
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("SELECT * FROM uregistration", con);
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
DataTable dt = new DataTable();
sda.Fill(dt);
object misValue = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
app.Visible = false;
Microsoft.Office.Interop.Excel.Workbook wb = app.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.ActiveSheet;
for(int i =0;i<dt.Columns.Count;i++)
{
ws.Cells[1, i + 1] = dt.Columns[i].ColumnName;
}
for(int i=0; i < dt.Rows.Count; i++)
{
for(int j=0; j < dt.Columns.Count; j++)
{
ws.Cells[i + 2, j + 1] = dt.Rows[i][j].ToString();
}
}
try {
ws.Name = dt.TableName;
}catch(Exception ex)
{
lbl.Text = ex.ToString();
}
wb.SaveAs("welcome2.xl", Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue,Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive,misValue,misValue,misValue,misValue,misValue);
wb.Close(true, misValue, misValue);
app.Quit();
}
}
Hope [this] (http://stackoverflow.com/questions/7099770/hresult-0x800a03ec-on-worksheet-range) hilft –
ist es besser, diese http://knowlton-group.com/using-ssis zu verwenden -to-export-data-to-excel/ – Karthikeyan
Starten Sie mit einer spezialisierten Bibliothek zum Erstellen von Excel-Dateien, wie zum Beispiel [EPPlus] (http://stackoverflow.com/documentation/epplus/drafts/98280). – VDWWD