【发布时间】:2014-10-27 13:01:39
【问题描述】:
我已经成功创建了一个带有互操作的 excel 文件,现在我希望下载该 excel 文件 在网络浏览器中而不是保存在驱动器中。
var xlApp = new Excel.Application();
Excel.Workbook xlWorkBook = xlApp.Workbooks.Add();
Excel.Worksheet xlWorkSheet = xlWorkBook.Sheets[1];
Excel.Range chartRange;
var attendance_list = CTX.schedules.Where(s => s.event_id == Convert.ToInt32(param));
if (attendance_list.Count() > 0)
{
xlWorkSheet.Cells[1, 1] = "PIC : ";
xlWorkSheet.Cells[2, 1] = "Tempat : ";
xlWorkSheet.Cells[1, 2] = attendance_list.FirstOrDefault().calendar_event.PIC;
xlWorkSheet.Cells[2, 2] = attendance_list.FirstOrDefault().calendar_event.sched_loc;
xlWorkSheet.Shapes.AddPicture(@Server.MapPath("~/Images/" + "oto_group2.png"), MsoTriState.msoFalse, MsoTriState.msoCTrue, 50, 50, 100, 45);
xlWorkSheet.Shapes.AddPicture(@Server.MapPath("~/Images/" + "oto_group3.png"), MsoTriState.msoFalse, MsoTriState.msoCTrue, 280, 50, 100, 45);
int row = 10;
int idx = 0;
xlWorkSheet.Cells[9, 1] = "No";
xlWorkSheet.Cells[9, 2] = "Name";
xlWorkSheet.Cells[9, 3] = "Phone";
xlWorkSheet.Cells[9, 4] = "Time";
xlWorkSheet.Cells[9, 5] = "Branch";
xlWorkSheet.Cells[9, 6] = "Sign";
xlWorkSheet.Cells[9, 7] = "Note";
chartRange = (xlWorkSheet.get_Range("b9"));
chartRange.ColumnWidth = 40;
chartRange = (xlWorkSheet.get_Range("c9"));
chartRange.ColumnWidth = 20;
chartRange = (xlWorkSheet.get_Range("g9"));
chartRange.ColumnWidth = 20;
var key = CTX.translate_value_ms.Where(t => t.PSF_type == "HRS_PHONE_TYPE"
&& t.value == "CELL").FirstOrDefault().translate_value_id;
foreach (var item in attendance_list)
{
idx++;
var hp = item.user_list.user_phones.Where(p => p.phone_type_id == key).FirstOrDefault();
xlWorkSheet.Cells[row, 1] = idx;
xlWorkSheet.Cells[row, 2] = displayFullName(item.user_list.fname, item.user_list.mname, item.user_list.lname);
xlWorkSheet.Cells[row, 3] = hp.phone_number;
xlWorkSheet.Cells[row, 4] = item.calendar_event.time_range;
xlWorkSheet.Cells[row, 5] = item.calendar_event.branch;
xlWorkSheet.Cells[row, 6] = "";
xlWorkSheet.Cells[row, 7] = "";
row++;
}
chartRange = xlWorkSheet.get_Range("a9", "g9");
chartRange.Font.Bold = true;
chartRange.Interior.Color = System.Drawing.Color.Gray;
chartRange = xlWorkSheet.get_Range("a9", "g" + (row - 1));
chartRange.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlMedium, Excel.XlColorIndex.xlColorIndexAutomatic, Excel.XlColorIndex.xlColorIndexAutomatic);
xlWorkBook.SaveAs("csharp.net-informations.xls", Excel.XlFileFormat.xlWorkbookNormal);
xlWorkBook.Close(true);
xlApp.Quit();
Marshal.ReleaseComObject(xlApp);
}
这是我迄今为止尝试创建的 excel 文件。需要一些启发。 “此代码将excel文件保存到驱动器,这不是我想要达到的结果。”
这是我生成的 excel:
【问题讨论】:
标签: c# asp.net visual-studio-2012 interop