![](/img/trans.png)
[英]How to write a text inside a rectangle in excel using Microsoft.Office.Interop.Excel in c#
[英]1) how to write excel to memorystream…2) how to download excel file from browser…using Micosoft.Office.Interop.Excel and c#
//i'm using VS 2008, dll as Microsoft.Office.Interop.Excel
//sample code..
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Add(misValue);//create new work book
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
//CREATING ONE Rectangle shape
xlWorkSheet.Shapes.AddShape(MsoAutoShapeType.msoShapeRectangle, 47, 24, 500, 90);
Microsoft.Office.Interop.Excel.Shape[] myShapes = new Microsoft.Office.Interop.Excel.Shape[1];
//adding textbox
myShapes[0] = xlWorkSheet.Shapes.AddTextbox(MsoTextOrientation.msoTextOrientationHorizontal, 75, 60, 70, 30);
myShapes[0].TextFrame.Characters(misValue, misValue).Text = "Tracking";
//從瀏覽器下載選項。
context.Response.ContentType = "application/vnd.ms-excel";
context.Response.AddHeader("Content-Disposition", "attachment; filename= report.xls");
//這里我正在使用臨時文本文件。但是我需要不使用臨時文件。
string file = context.Server.MapPath("new.txt");
Byte[] myByte = File.ReadAllBytes(file);
context.Response.Clear();
context.Response.BinaryWrite(myByte);
context.Response.Flush();
context.Response.Close();
我們可以使用Ipersiststream接口將對象保存在memorystream中嗎?
如果您有興趣創建格式比GridView更多的Excel文檔,則可以看一下EPPlus:
該庫能夠創建具有自定義單元格,格式等功能的Excel文件。
如果GridView適合您,請嘗試使用它,但如果沒有,則還有其他Excel選項。 您應該能夠將內容保存到內存流中並將該內存流輸出到瀏覽器。
這是我與ASP.NET MVC一起使用的FileResult處理程序的示例。 該概念可以應用於WebForms,它所做的只是寫到響應流中。 我的班級有一個要加載的數據表的屬性(this.dataTable)和要瀏覽器下載的文件名(fileName)和工作簿名稱(this.workBookName)
protected override void WriteFile(HttpResponseBase response)
{
using (ExcelPackage pck = new ExcelPackage())
{
ExcelWorksheet ws = pck.Workbook.Worksheets.Add(this.workBookName);
ws.Cells["A1"].LoadFromDataTable(this.dataTable, true);
response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
response.AddHeader("content-disposition", "attachment; filename=" + fileName);
response.BinaryWrite(pck.GetAsByteArray());
}
}
我完全同意約翰·桑德斯的觀點,也許您應該嘗試這種方式。
將數據集綁定到GridView,然后將其下載到excel。
使用下面的代碼段從控件獲取值
private static void PrepareControlForExport(Control control)
{
for (int i = 0; i < control.Controls.Count; i++)
{
Control current = control.Controls[i];
if (current is LinkButton)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text));
}
else if (current is ImageButton)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText));
}
else if (current is HyperLink)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text));
}
else if (current is DropDownList)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text));
}
else if (current is CheckBox)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False"));
}
if (current.HasControls())
{
PrepareControlForExport(current);
}
}
}
現在,創建一個將從gridview下載數據的方法。
private void ExportX()
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; Filename = ExcelReport.xls"));
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.ContentType = "application/ms-excel";
StringWriter stringWrite = new StringWriter();
HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWrite);
Table table = new Table();
// include the gridline settings
table.GridLines = GridView1.GridLines;
if (GridView1.HeaderRow != null)
{
PrepareControlForExport(GridView1.HeaderRow);
table.Rows.Add(GridView1.HeaderRow);
}
foreach (GridViewRow row in GridView1.Rows)
{
PrepareControlForExport(row);
table.Rows.Add(row);
}
if (GridView1.FooterRow != null)
{
PrepareControlForExport(GridView1.HeaderRow);
table.Rows.Add(GridView1.FooterRow);
}
table.RenderControl(htmlWriter);
Response.Write(stringWrite.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
}
這就是您需要做的,調用上面的方法下載數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.