Aspose.Cells小实例

Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook();
Aspose.Cells.Worksheet sheet = workbook.Worksheets[0];
sheet.FreezePanes(1, 1, 1, 0);//冻结第一行

sheet.Cells["A1"].PutValue("ID");
sheet.Cells["B1"].PutValue("手机号码");
sheet.Cells["C1"].PutValue("姓名");
sheet.Cells["D1"].PutValue("出生年月");
sheet.Cells["E1"].PutValue("性别");
sheet.Cells["F1"].PutValue("订购份数");
sheet.Cells["G1"].PutValue("运营产品ID");
sheet.Cells["H1"].PutValue("订单状态");
sheet.Cells["I1"].PutValue("订单成功时间");
sheet.Cells["J1"].PutValue("批次ID");
sheet.Cells["K1"].PutValue("支付方式");
sheet.Cells["L1"].PutValue("错误代码");

///TODO
///设置列1为文本,因为这列全是数字而且很长,不处理会变成自然数了。
///这里需要注意Style是设置风格,而StyleFlag是开关,所以即使你设置了Style,没有打开对应的StyleFlag一样没用
Aspose.Cells.Style sc1 = workbook.Styles[workbook.Styles.Add()];
sc1.ShrinkToFit = true;
sc1.Number = 49;

Aspose.Cells.StyleFlag scf1 = new Aspose.Cells.StyleFlag();
scf1.ShrinkToFit = true;
scf1.NumberFormat = true;
Aspose.Cells.Column colomn1 = sheet.Cells.Columns[1];
colomn1.ApplyStyle(sc1, scf1);

dt =getDataTable();//得到DataTable
sheet.Cells.ImportDataTable(dt, false, "A2");//从A2开始填充数据
sheet.AutoFitColumns();//让各列自适应宽度,这个很有用。
workbook.Save(fileName, Aspose.Cells.FileFormatType.Excel2007Xlsx, Aspose.Cells.SaveType.OpenInExcel, response);//输出



很多时候输出的数据排序或者显示的列并不一定和DataTable得到的列排序数据完全一致,那么我们可以简单处理一下:
比如把上面的

引用内容 引用内容
///TODO
///设置列1为文本,因为这列全是数字而且很长,不处理会变成自然数了。
///这里需要注意Style是设置风格,而StyleFlag是开关,所以即使你设置了Style,没有打开对应的StyleFlag一样没用
Aspose.Cells.Style sc1 = workbook.Styles[workbook.Styles.Add()];
sc1.ShrinkToFit = true;
sc1.Number = 49;

Aspose.Cells.StyleFlag scf1 = new Aspose.Cells.StyleFlag();
scf1.ShrinkToFit = true;
scf1.NumberFormat = true;
Aspose.Cells.Column colomn1 = sheet.Cells.Columns[1];
colomn1.ApplyStyle(sc1, scf1);

dt =getDataTable();//得到DataTable


替换成:

引用内容 引用内容
DataTable dt1=getDataTable();
for (int i = 0; i < dt1.Rows.Count; i++)
{
sheet.Cells[(i + 1), 0].PutValue(dt1.Rows[i]["create_time"].ToString());
sheet.Cells[(i + 1), 1].PutValue(dt1.Rows[i]["holder_mobile"].ToString());
sheet.Cells[(i + 1), 2].PutValue(dt1.Rows[i]["rec_name"].ToString());
sheet.Cells[(i + 1), 3].PutValue(string.IsNullOrEmpty(dt1.Rows[i]["rec_sex"].ToString()) ? "" : (dt1.Rows[i]["rec_sex"].ToString() == "1") ? "男" : "女");
sheet.Cells[(i + 1), 4].PutValue(dt1.Rows[i]["rec_birthday"].ToString());
sheet.Cells[(i + 1), 5].PutValue(dt1.Rows[i]["base_product_code"].ToString());
sheet.Cells[(i + 1), 6].PutValue(dt1.Rows[i]["sell_price"].ToString());
sheet.Cells[(i + 1), 7].PutValue(dt1.Rows[i]["pay_count"].ToString());
sheet.Cells[(i + 1), 8].PutValue(dt1.Rows[i]["ins_policy"].ToString());
sheet.Cells[(i + 1), 9].PutValue(dt1.Rows[i]["ins_end_date"].ToString());
sheet.Cells[(i + 1), 10].PutValue(Rondi.Insu.Management.Utility.StateEnum.OrderFrom(dt1.Rows[i]["order_from"].ToString()));
sheet.Cells[(i + 1), 11].PutValue(Rondi.Insu.Management.Utility.StateEnum.OrderState(dt1.Rows[i]["order_state"].ToString()));
}
posted @ 2012-07-20 11:24  踏浪帅  阅读(3488)  评论(0编辑  收藏  举报