Aspose.Cells 导入导出EXCEL
文章出处:http://hi.baidu.com/leilongbing/item/c11467e1819e5417595dd8c1
修改样式
Workbook workbook = new Workbook(); //工作簿
Worksheet sheet = workbook.Worksheets[0]; //工作表
Cells cells = sheet.Cells;//单元格
//样式2
Aspose.Cells.Style style2 = workbook.Styles[workbook.Styles.Add()];//新增样式
style2.HorizontalAlignment = TextAlignmentType.Center;//文字居中
style2.VerticalAlignment = TextAlignmentType.Center;
style2.Font.Name = "宋体";//文字字体
style2.Font.Size = 12;//文字大小
style2.Font.IsBold = true;//粗体
style3.BackgroundColor = System.Drawing.Color.Yellow;
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;
cells.SetRowHeight(3, 10);//设高
cells.SetColumnWidth(Colnum - 1, 50);//设宽
cells[0, 2].Style = style2;//0表示行号,2表示列号
cells.Merge(1, 2, 3, 4);//合并单元格 1表示行号,2表示列号,3表示合并的行号,4表示合并的列数; 把3或者4其中一个改变成1 ,表示不合并行或者列;如cells.Merge(1, 2, 3, 1);只合并三行,不合并列
/// <summary>
/// 导出方法
/// </summary>
/// <param name="mcorinfo">信息一</param>
/// <param name="ds">信息二</param>
public void ToExcel(M_CorporationCirclesInfo mcorinfo,DataSet ds)
{
Aspose.Cells.Workbook wk = new Aspose.Cells.Workbook();
wk.Open(System.Web.HttpContext.Current.Server.MapPath(@"~\Excel\abc.xls"));
AddExcelSHeet1(wk, mcorinfo);
AddExcelSHeet2(wk, ds);
wk.Save(System.Web.HttpContext.Current.Server.MapPath(@"~\Excel\abcd.xls"));
// wk.Open(System.Web.HttpContext.Current.Server.MapPath(@"~\Excel\abcd.xls"));
// Response.Write( wk.Worksheets[0].Cells[2, 1].Value.ToString()); //红色的01表示第1个Sheet ,Cells[i,k] i表示第几行 k 表示第几列 Value 是往里面读值
}
/// <summary>
/// 导出信息一
/// </summary>
/// <param name="cells">当前EXCEL SHEET行号</param>
/// <param name="mcorinfo">信息一</param>
private void AddExcelSHeet1(Aspose.Cells.Workbook wk, M_CorporationCirclesInfo mcorinfo)
{
wk.Worksheets[0].Cells[2, 1].PutValue(mcorinfo.CompanySystem.ToString());
wk.Worksheets[0].Cells[3, 1].PutValue(mcorinfo.EnrolName.ToString());
wk.Worksheets[0].Cells[3, 6].PutValue(mcorinfo.EnroNumber.ToString());
wk.Worksheets[0].Cells[4, 1].PutValue(mcorinfo.Principal.ToString());
wk.Worksheets[0].Cells[4, 6].PutValue(mcorinfo.OrgnaztionCode.ToString());
wk.Worksheets[0].Cells[5, 1].PutValue(mcorinfo.Place.ToString());
wk.Worksheets[0].Cells[5, 6].PutValue(mcorinfo.EnroTime.ToString());
wk.Worksheets[0].Cells[6, 1].PutValue(mcorinfo.DeaIinLieu.ToString());
wk.Worksheets[0].Cells[6, 6].PutValue(mcorinfo.DealinTerm.ToString());
wk.Worksheets[0].Cells[7, 1].PutValue(mcorinfo.EnroCapital.ToString());
wk.Worksheets[0].Cells[7, 6].PutValue(mcorinfo.PaiclupCapital.ToString());
wk.Worksheets[0].Cells[8, 1].PutValue(mcorinfo.EnroDepartment.ToString());
wk.Worksheets[0].Cells[8, 6].PutValue(mcorinfo.EnroAddress.ToString());
wk.Worksheets[0].Cells[10, 1].PutValue(mcorinfo.ChurchyardTypeCode.ToString());
wk.Worksheets[0].Cells[10, 2].PutValue(mcorinfo.FremdnessTypeCodeID.ToString());
wk.Worksheets[0].Cells[10, 3].PutValue(mcorinfo.FremdnessTypeCode.ToString());
wk.Worksheets[0].Cells[9, 5].PutValue(mcorinfo.EnroTypeCode.ToString());
wk.Worksheets[0].Cells[10, 7].PutValue(mcorinfo.IndustryType.ToString());
wk.Worksheets[0].Cells[10, 8].PutValue(mcorinfo.IndustryLittleType.ToString());
wk.Worksheets[0].Cells[11, 1].PutValue(mcorinfo.DealinBound.ToString());
wk.Worksheets[0].Cells[13, 1].PutValue(mcorinfo.CorporationSort.ToString());
wk.Worksheets[0].Cells[13, 4].PutValue(mcorinfo.ManageLeves.ToString());
wk.Worksheets[0].Cells[13, 6].PutValue(mcorinfo.SuperiorManage.ToString());
wk.Worksheets[0].Cells[14, 1].PutValue(mcorinfo.AptitudeCircs.ToString());
wk.Worksheets[0].Cells[15, 1].PutValue(mcorinfo.Qualify.ToString());
wk.Worksheets[0].Cells[16, 1].PutValue(mcorinfo.ManageDepart.ToString());
wk.Worksheets[0].Cells[16, 4].PutValue(mcorinfo.LinkMan.ToString());
wk.Worksheets[0].Cells[16, 7].PutValue(mcorinfo.Contact.ToString());
wk.Worksheets[0].Cells[17, 1].PutValue(mcorinfo.LinkAddress.ToString());
wk.Worksheets[0].Cells[17, 5].PutValue(mcorinfo.InternetAddress.ToString());
wk.Worksheets[0].Cells[18, 1].PutValue(mcorinfo.Rmark.ToString());
}
/// <summary>
/// 导出增加信息二
/// </summary>
/// <param name="cells">当前EXCEL SHEET行号<</param>
/// <param name="ds">信息内容</param>
private void AddExcelSHeet2(Aspose.Cells.Workbook wk, DataSet ds)
{
if (ds != null)
{
DataRow[] werow = ds.Tables[0].Select(" Stockholderkind=1");
if (werow.Length > 0)
{
int i = 2;
foreach (DataRow rows in werow)//查询
{
i++;
wk.Worksheets[1].Cells[i, 1].PutValue(rows["StockholdeName"].ToString());
wk.Worksheets[1].Cells[i, 2].PutValue(rows["Stock"].ToString()); }
}
DataRow[] yourow = ds.Tables[0].Select(" Stockholderkind=2");
if (yourow.Length > 0)
{
int i = 2;
foreach (DataRow rows in yourow)//查询
{
i++;
wk.Worksheets[1].Cells[i, 4].PutValue(rows["StockholdeName"].ToString());
wk.Worksheets[1].Cells[i, 5].PutValue(rows["IsOr"].ToString());
wk.Worksheets[1].Cells[i, 6].PutValue(rows["Stock"].ToString()); //红色的1表示第二个Sheet ,Cells[i,k] i表示第几行 k 表示第几列 PutValue 是往里面写值
}
}
}
}
/// <summary>
/// 导入方法
/// </summary>
public void ReadExcelTrade()
{
Aspose.Cells.Workbook wk = new Aspose.Cells.Workbook();
wk.Open(System.Web.HttpContext.Current.Server.MapPath(@"~\Excel\类型与代码.xls"));// 这儿是需要导入的文件
M_CorporationRegisteredTrade mtrade = new M_CorporationRegisteredTrade();
B_CorporationRegisteredTrade btrade = new B_CorporationRegisteredTrade();
for (int i = 1; i <= 149; i++)//用于EXCEL数据的等号,可以自行固定如:149,也可以自行去读取它的等号;
{
if (i > 149)
{
mtrade.CorporationTypeCode = wk.Worksheets[0].Cells[i, 0].Value.ToString().Trim();//读取文件里面对应的信息
mtrade.Type_Name = wk.Worksheets[0].Cells[i, 1].Value.ToString().Trim();
mtrade.P_CorporationTypeCode = wk.Worksheets[0].Cells[i, 2].Value.ToString().Trim();
mtrade.Level = wk.Worksheets[0].Cells[i, 3].Value.ToString().Trim();
mtrade.Rmark = "";
btrade.Add(mtrade);
}
}
}
Aspose.Cells.dll 所需的引用