(C \ #) Aspose. Cells 구성 요 소 를 이용 하여 엑셀 파일 내 보 내기 가 져 오기
1768 단어 B / S 응용 프로그램C / S 응용 프로그램
가 져 오기:
public static System.Data.DataTable ReadExcel(String strFileName)
{
Workbook book = new Workbook();
book.Open(strFileName);
Worksheet sheet = book.Worksheets[0];
Cells cells = sheet.Cells;
return cells.ExportDataTableAsString(0, 0, cells.MaxDataRow + 1, cells.MaxDataColumn + 1, true);
}
내 보 내기:
private static void Export(IEnumerable data, HttpResponse response)
{
Workbook workbook = new Workbook();
Worksheet sheet = (Worksheet)workbook.Worksheets[0];
PropertyInfo[] ps = typeof(T).GetProperties();
var colIndex = "A";
foreach (var p in ps)
{
sheet.Cells[colIndex + 1].PutValue(p.Name);
int i = 2;
foreach (var d in data)
{
sheet.Cells[colIndex + i].PutValue(p.GetValue(d, null));
i++;
}
colIndex = ((char)(colIndex[0] + 1)).ToString();
}
response.Clear();
response.Buffer = true;
response.Charset = "utf-8";
response.AppendHeader("Content-Disposition", "attachment;filename=xxx.xls");
response.ContentEncoding = System.Text.Encoding.UTF8;
response.ContentType = "application/ms-excel";
response.BinaryWrite(workbook.SaveToStream().ToArray());
response.End();
}
아주 간단 해, 쓰기 좋아!