c#excelをテンプレートからエクスポート
2598 ワード
private void Bind()
{
//
string TempletFileName = Server.MapPath("template.xls");
//
string ReportFileName = Server.MapPath("out.xls");
string strTempletFile = Path.GetFileName(TempletFileName);
//
FileInfo mode = new FileInfo(TempletFileName);
mode.CopyTo(ReportFileName, true);
// excel
object missing = Missing.Value;
Application app = null;
Workbook wb = null;
Worksheet ws = null;
Range r = null;
//
app = new Microsoft.Office.Interop.Excel.Application();
wb = app.Workbooks.Open(ReportFileName, false, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
app.Visible = true;
// WorkSheet
ws = (Worksheet)wb.Worksheets.get_Item(1);
// WorkSheet
ws.Cells[1, 1] = "100";
ws.Cells[2, 1] = "100";
ws.Cells[2, 2] = "100";
//
r = (Range)ws.Cells[2, 3];
r.Formula = "=A2*B2";
// Excel
wb.Save();
wb.Close(null, null, null);
app.Workbooks.Close();
app.Application.Quit();
app.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(ws);
System.Runtime.InteropServices.Marshal.ReleaseComObject(wb);
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
ws = null;
wb = null;
app = null;
string path = Server.MapPath("out.xls");
System.IO.FileInfo file = new System.IO.FileInfo(path);
Response.Clear();
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF8;
// , " / "
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(" .xls"));
// , ,
Response.AddHeader("Content-Length", file.Length.ToString());
// ,
Response.ContentType = "application/ms-excel";
//
Response.WriteFile(file.FullName);
//
Response.End();
}
COM , :Microsoft.Excel.x.0.Object.Library,Microsoft.Office.x.0.Object.Library
OFFICE, 11.0 (Office2003 ), Com , Excel、Microsoft.Office.Core,VBIDE Library.