標籤:style class code http tar ext
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 ].PutValue("樣式使用")
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 所需的引用