2015-11-05 54 views
-1

我只能在頂部寫入不變數據(例如.company名稱)。我想寫在底部。在Excel底部導出不變數據

將數據導出到Excel時,如何在導出的所有數據的底部寫入一組不變數據?

的代碼(從評論):

String strFileName = "ABCReport.xls"; 
Response.ClearContent(); 
Response.AddHeader("Content-Disposition", "attachment; filename=\"" + strFileName + "\""); 
Response.ContentType = "application/excel"; 
System.IO.StringWriter sw = new System.IO.StringWriter(); 
HtmlTextWriter htw = new HtmlTextWriter(sw); 
htw.WriteLine("<b><u><font size='4'>" + "Company name" + " </font></u></b>"); 
htw.WriteLine("<br>"); 
dg.RenderControl(htw); Response.Write(sw.ToString()); 
Response.End(); 
+0

後你已經嘗試 – MusicLovingIndianGirl

+0

代碼也做SO或谷歌爲這個簡單的搜索。就在2秒鐘之前,還有另外一個類似的問題。提示:檢查EPPlus庫 –

+1

以下是代碼。 – Shwetha

回答

0
// Create connection string variable. Modify the "Data Source" 
// parameter as appropriate for your environment. 
String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + 
    "Data Source=" + Server.MapPath("../ExcelData.xls") + ";" + 
    "Extended Properties=Excel 8.0;"; 

// Create connection object by using the preceding connection string. 
OleDbConnection objConn = new OleDbConnection(sConnectionString); 

// Open connection with the database. 
objConn.Open(); 

// The code to follow uses a SQL SELECT command to display the data from the worksheet. 

// Create new OleDbCommand to return data from worksheet. 
OleDbCommand objCmdSelect =new OleDbCommand("SELECT * FROM myRange1", objConn); 

// Create new OleDbDataAdapter that is used to build a DataSet 
// based on the preceding SQL SELECT statement. 
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter(); 

// Pass the Select command to the adapter. 
objAdapter1.SelectCommand = objCmdSelect; 

// Create new DataSet to hold information from the worksheet. 
DataSet objDataset1 = new DataSet(); 

// Fill the DataSet with the information from the worksheet. 
objAdapter1.Fill(objDataset1, "XLData"); 

// Bind data to DataGrid control. 
DataGrid1.DataSource = objDataset1.Tables[0].DefaultView; 
DataGrid1.DataBind();  

// Clean up objects. 
objConn.Close(); 
+0

我已經將數據綁定到datagrid。我想寫入不變的數據寫在excel的底部。 – Shwetha

+0

很難捍衛者:) –