我创建了一个Excel工作簿。我可以将此excel工作簿直接转换为内存流或字节数组以通过HTTP传递吗?我不想保存Excel工作簿。我需要通过angular js下载此excel文件。我正在使用Web API 2
using Excel = Microsoft.Office.Interop.Excel;
private MemoryStream ExportDataSetToExcel()
{
System.Data.DataTable employeeTable = new System.Data.DataTable("Employee");
employeeTable.Columns.Add("Employee ID");
employeeTable.Columns.Add("Employee Name");
employeeTable.Rows.Add("1", "ABC");
employeeTable.Rows.Add("2", "DEF");
employeeTable.Rows.Add("3", "PQR");
employeeTable.Rows.Add("4", "XYZ");
////Create a Department Table
System.Data.DataTable departmentTable = new System.Data.DataTable("Department");
departmentTable.Columns.Add("Department ID");
departmentTable.Columns.Add("Department Name");
departmentTable.Rows.Add("1", "IT");
departmentTable.Rows.Add("2", "HR");
departmentTable.Rows.Add("3", "Finance");
////Create a DataSet with the existing DataTables
DataSet ds = new DataSet("Organization");
ds.Tables.Add(employeeTable);
ds.Tables.Add(departmentTable);
////Creae an Excel application instance
Excel.Application excelApp = new Excel.Application();
excelApp.Visible = true;
Excel.Workbook excelWorkBook = excelApp.Workbooks.Add();
foreach (System.Data.DataTable table in ds.Tables)
{
////Add a new worksheet to workbook with the Datatable name
Microsoft.Office.Interop.Excel.Worksheet excelWorkSheet = excelWorkBook.Sheets.Add();
excelWorkSheet.Name = table.TableName;
for (int i = 1; i < table.Columns.Count + 1; i++)
{
excelWorkSheet.Cells[1, i] = table.Columns[i - 1].ColumnName;
excelWorkSheet.Cells[1, i].Font.Bold = true;
}
for (int j = 0; j < table.Rows.Count; j++)
{
for (int k = 0; k < table.Columns.Count; k++)
{
excelWorkSheet.Cells[j + 2, k + 1] = table.Rows[j].ItemArray[k].ToString();
excelWorkSheet.Cells[j + 2, k + 1].Interior.Color = Excel.XlRgbColor.rgbRed;
}
}
}
MemoryStream ms = new MemoryStream();
////excelWorkBook.SaveAs(ms, Type.Missing, Type.Missing, Type.Missing, true, false, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing);
excelWorkBook.Close();
excelApp.Quit();
return ms;
}
答案 0 :(得分:1)
您不能直接转换。您必须将文件保存到临时路径
from bokeh.io import show
from bokeh.models import TapTool, CustomJS, ColumnDataSource
from bokeh.plotting import figure
base_xs = [0, 1, 1, 0]
base_ys = [0, 0, 1, 1]
def offset(base, d):
return [i + d for i in base]
ds = ColumnDataSource(dict(xs=[[[offset(base_xs, dx)]] for dx in base_xs],
ys=[[[offset(base_ys, dy)]] for dy in base_ys],
color=['red', 'green', 'blue', 'yellow']))
tt = TapTool(behavior='inspect')
p = figure(tools=[tt, 'reset'])
p.multi_polygons('xs', 'ys', color='color', source=ds)
tt.callback = CustomJS(args=dict(x_range=p.x_range, y_range=p.y_range),
code="""\
const {source} = cb_data;
const idx = source.inspected.indices[0];
const {xs, ys} = source.data;
const poly_xs = xs[idx].flat(2);
const poly_ys = ys[idx].flat(2);
x_range.start = Math.min(...poly_xs);
x_range.end = Math.max(...poly_xs);
y_range.start = Math.min(...poly_ys);
y_range.end = Math.max(...poly_ys);
""")
show(p)