Aspose.CellsはExcelサーバーを生成してofficeをインストールしない
5482 ワード
Excelを生成してブラウザでダウンロードする方法:
テーブルを作成し、Excelダウンロードの生成を呼び出す
///
///
///
/// DataTable
///
///
public static void Export(DataTable dt, string sheetName, HttpResponseBase Response)
{
Workbook workbook = new Workbook();//
Worksheet sheet = (Worksheet)workbook.Worksheets[0];//
sheet.Name = sheetName;
Cells cells = sheet.Cells;//
//
Style styleTitle = workbook.Styles[workbook.Styles.Add()];//
styleTitle.HorizontalAlignment = TextAlignmentType.Center;//
styleTitle.Font.Name = " ";//
styleTitle.Font.Size = 15;//
styleTitle.Font.IsBold = true;//
// 2
Style style2 = workbook.Styles[workbook.Styles.Add()];//
style2.HorizontalAlignment = TextAlignmentType.Center;//
style2.Font.Name = " ";//
style2.Font.Size = 12;//
style2.Font.IsBold = true;//
//style2.IsTextWrapped = true;//
style2.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;
style2.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;
style2.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;
style2.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;
// 3
Style style3 = workbook.Styles[workbook.Styles.Add()];//
style3.HorizontalAlignment = TextAlignmentType.Left;//
style3.Font.Name = " ";//
style3.Font.Size = 11;//
style3.IsTextWrapped = true;//
style3.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;
style3.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;
style3.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;
style3.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;
int Colnum = dt.Columns.Count;//
int Rownum = dt.Rows.Count;//
// 1
cells.Merge(0, 0, 1, Colnum);//
cells[0, 0].PutValue(sheetName);//
cells[0, 0].SetStyle(styleTitle);
cells.SetRowHeight(0, 35);
// 2
for (int i = 0; i < Colnum; i++)
{
cells[1, i].PutValue(dt.Columns[i].ColumnName);
cells[1, i].SetStyle(style2);
cells.SetRowHeight(1, 25);//
//
//cells.SetColumnWidth(i, 30);
}
//
for (int i = 0; i < Rownum; i++)
{
for (int k = 0; k < Colnum; k++)
{
cells[2 + i, k].PutValue(dt.Rows[i][k].ToString());
cells[2 + i, k].SetStyle(style3);
}
cells.SetRowHeight(2 + i, 20);
}
sheet.AutoFitColumns();//
//sheet.AutoFitRows();//
// Excel
Response.Clear();
Response.Buffer = true;
Response.Charset = "utf-8";
string filename = HttpUtility.UrlEncode(DateTime.Now.ToString(sheetName));
Response.AppendHeader("Content-Disposition", "attachment;filename=" + filename + ".xls");
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.ContentType = "application/ms-excel";
Response.BinaryWrite(workbook.SaveToStream().ToArray());
Response.End();
Response.Close();
}
テーブルを作成し、Excelダウンロードの生成を呼び出す
List activityList = ActivityResult.Data;
if (activityList.Count>0)
{
//
DataTable dt = new DataTable();
dt.Columns.Add(" ");
dt.Columns.Add(" ");
dt.Columns.Add(" ( )");
dt.Columns.Add(" ( )");
dt.Columns.Add(" ");
dt.Columns.Add(" ( )");
dt.Columns.Add(" ( )");
dt.Columns.Add(" ");
//
foreach (ActivityStatisticsModel activity in activityList)
{
DataRow dr = dt.NewRow();
dr[" "] = activity.ActivityName;
dr[" "] = activity.BrowseCount;
dr[" ( )"] = activity.AnderBroCount;
dr[" ( )"] = activity.IosBroCount;
dr[" "] = activity.ShareCount;
dr[" ( )"] = activity.AnderShaCount;
dr[" ( )"] = activity.IosShaCount;
dr[" "] = activity.TotalCount;
dt.Rows.Add(dr);
}
// Excel
OfficeOperation.Export(dt, " ", Response);
}