C#实现Excel导出
Excel导出广泛应用于管理、统计、金融等众多领域。
C#实现Excel导出需要引用Aspose.Cells。
Excel导出方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Aspose.Cells;
using System.Data;
public class ExcelFile
{
public static Workbook GetWorkBook()
{
Workbook workbook = new Workbook();
return workbook;
}
public static System.IO.MemoryStream OutStream(Workbook workbook, DataTable dataTable, string tableName, ExcelStyle exStyle)
{
Worksheet sheet = workbook.Worksheets[0];
Cells cells = sheet.Cells;
int Colnum = dataTable.Columns.Count;
int Rownum = dataTable.Rows.Count;
int index = 0;
if (tableName != null)
{
cells.Merge(0, 0, 1, Colnum);
cells[index, 0].PutValue(tableName);
if (exStyle.TableNameStyle != null)
{
cells[index, 0].SetStyle(exStyle.TableNameStyle);
}
index++;
cells.SetRowHeight(0, 38);
}
for (int i = 0; i < Colnum; i++)
{
cells[index, i].PutValue(dataTable.Columns[i].ColumnName);
if (exStyle.TitleStyle != null)
{
cells[index, i].SetStyle(exStyle.TitleStyle);
}
cells.SetRowHeight(index, 25);
}
index++;
for (int i = 0; i < Rownum; i++)
{
for (int k = 0; k < Colnum; k++)
{
cells[index + i, k].PutValue(dataTable.Rows[i][k].ToString());
if (exStyle.ContentStyle != null)
{
cells[index + i, k].SetStyle(exStyle.ContentStyle);
}
}
cells.SetRowHeight(index + i, 24);
}
int columnCount = cells.MaxColumn;
int rowCount = cells.MaxRow;
for (int col = 0; col < columnCount; col++)
{
sheet.AutoFitColumn(col, 0, rowCount);
}
System.IO.MemoryStream ms = workbook.SaveToStream();
return ms;
}
public static System.IO.MemoryStream OutStream(Workbook workbook, DataTable dataTable, string tableName, string reportInfo, ExcelStyle exStyle)
{
Worksheet sheet = workbook.Worksheets[0];
Cells cells = sheet.Cells;
int Colnum = dataTable.Columns.Count;
int Rownum = dataTable.Rows.Count;
int index = 0;
if (tableName != null)
{
cells.Merge(0, 0, 1, Colnum);
cells[index, 0].PutValue(tableName);
if (exStyle.TableNameStyle != null)
{
cells[index, 0].SetStyle(exStyle.TableNameStyle);
}
index++;
cells.SetRowHeight(0, 38);
}
if (reportInfo != null)
{
cells.Merge(1, 0, 1, Colnum);
cells[index, 0].PutValue(reportInfo);
if (exStyle.TableNameStyle != null)
{
cells[index, 0].SetStyle(exStyle.ReprotInfoStyle);
}
index++;
cells.SetRowHeight(0, 38);
}
for (int i = 0; i < Colnum; i++)
{
cells[index, i].PutValue(dataTable.Columns[i].ColumnName);
if (exStyle.TitleStyle != null)
{
cells[index, i].SetStyle(exStyle.TitleStyle);
}
cells.SetRowHeight(index, 25);
}
index++;
for (int i = 0; i < Rownum; i++)
{
for (int k = 0; k < Colnum; k++)
{
cells[index + i, k].PutValue(dataTable.Rows[i][k].ToString());
if (exStyle.ContentStyle != null)
{
cells[index + i, k].SetStyle(exStyle.ContentStyle);
}
}
cells.SetRowHeight(index + i, 24);
}
int columnCount = cells.MaxColumn;
int rowCount = cells.MaxRow;
for (int col = 0; col < columnCount; col++)
{
sheet.AutoFitColumn(col, 0, rowCount);
}
for (int col = 0; col < columnCount; col++)
{
cells.SetColumnWidthPixel(col, cells.GetColumnWidthPixel(col) + 30);
}
System.IO.MemoryStream ms = workbook.SaveToStream();
return ms;
}
}
public class ExcelStyle
{
private Style tableNameStyle;
public Style TableNameStyle
{
get { return tableNameStyle; }
set { tableNameStyle = value; }
}
private Style reprotInfoStyle;
public Style ReprotInfoStyle
{
get { return reprotInfoStyle; }
set { reprotInfoStyle = value; }
}
private Style titleStyle;
public Style TitleStyle
{
get { return titleStyle; }
set { titleStyle = value; }
}
private Style contentStyle;
public Style ContentStyle
{
get { return contentStyle; }
set { contentStyle = value; }
}
}
调用方法
public void Excel(){
DataTable dataTable = dataTable ();
Aspose.Cells.Workbook workbook = ExcelFile.GetWorkBook();
Aspose.Cells.Style styleTitle = workbook.Styles[workbook.Styles.Add()];
styleTitle.HorizontalAlignment = TextAlignmentType.Center;
styleTitle.Font.Name = "宋体";
styleTitle.Font.Size = 18;
styleTitle.Font.IsBold = true;
Aspose.Cells.Style style2 = workbook.Styles[workbook.Styles.Add()];
style2.HorizontalAlignment = TextAlignmentType.Center;
style2.Font.Name = "宋体";
style2.Font.Size = 14;
style2.Font.IsBold = true;
style2.IsTextWrapped = false;
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;
Aspose.Cells.Style style3 = workbook.Styles[workbook.Styles.Add()];
style3.HorizontalAlignment = TextAlignmentType.Center;
style3.Font.Name = "宋体";
style3.Font.Size = 12;
style3.IsTextWrapped = false;
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;
ExcelStyle exStyle = new ExcelStyle();
exStyle.ContentStyle = style3;
exStyle.TitleStyle = style2;
exStyle.TableNameStyle = styleTitle;
System.IO.MemoryStream ms = ExcelFile.OutStream(workbook, dataTable, "Excel文件名", exStyle);
HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.Buffer = true;
response.Charset = "utf-8";
response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpContext.Current.Server.UrlEncode("Excel文件名.xls"));
response.ContentEncoding = System.Text.Encoding.UTF8;
response.ContentType = "application/ms-excel; charset=UTF-8 ";
response.BinaryWrite(ms.ToArray());
response.End();
}
例:
执行SQL语句如下: 导出至Excel文件信息如下:
|