/*转载请注明出处 谢谢*/
研究了下Aspose.cells 操作Excel代码 总结下大致的使用
1、 Aspose.cells 插入图片(HTTP 方式)
private void button1_Click(object sender, EventArgs e)
{
System.IO.MemoryStream objImage;
System.Net.WebClient objwebClient;
string sURL = "http://127.0.0.1/45C.jpg"; //采用的http访问图片的方式
objwebClient = new System.Net.WebClient();
objImage = new System.IO.MemoryStream(objwebClient.DownloadData(sURL));
Aspose.Cells.Workbook wb = new Aspose.Cells.Workbook(); //
Aspose.Cells.Worksheet sheet = wb.Worksheets[0];
Aspose.Cells.Drawing.PictureCollection pictures = sheet.Pictures;
pictures.Add(0, 0,3,3 ,objImage);
wb.Save("d:\\sysdata\\webimagebook.xls");
}
2. 指定行的格式复制
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
Aspose.Cells.Workbook wb = new Aspose.Cells.Workbook("D://Sysdata//1234.xls");
Aspose.Cells.Worksheet sheet = wb.Worksheets["Sheet1"];
Cells cells = sheet.Cells;
//0 复制开始行 8 粘贴起始行 4指定行数(Execl中行号加1)
sheet.Cells.CopyRows(cells,0,8,4);
wb.Save("D://Sysdata//1234.xls");
MessageBox.Show("保存完成");
}
3.Aspose.cells 单元格 格式设置
/// <summary>
/// 格式设置
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button5_Click(object sender, EventArgs e)
{
Aspose.Cells.Workbook wb = new Aspose.Cells.Workbook("D://Sysdata//1234.xls");
Aspose.Cells.Worksheet sheet = wb.Worksheets["Sheet1"];
Cells cells = sheet.Cells;
Style style = CreateAsponeStyle(wb); //创建格式
sheet.Cells[11, 2].PutValue("九五智障");
sheet.Cells[11, 2].SetStyle(style); //单元格格式设置
wb.Save("D://Sysdata//1234.xls");
MessageBox.Show("执行完成");
}
/// <summary>
/// 创建单元格格式
/// </summary>
/// <param name="wb"></param>
/// <returns></returns>
public Style CreateAsponeStyle(Workbook wb)
{
Style style = wb.CreateStyle();
//设置居中
style.HorizontalAlignment = Aspose.Cells.TextAlignmentType.Center;
//设置背景颜色
style.ForegroundColor = System.Drawing.Color.FromArgb(153, 204, 0);
//单元格的线:实线
style.Pattern = Aspose.Cells.BackgroundType.Solid;
//字体居中
style.HorizontalAlignment = Aspose.Cells.TextAlignmentType.Center;
//文字字体
style.Font.Name = "宋体";
//文字大小
style.Font.Size = 36;
//单元格内容自动换行
style.IsTextWrapped = true;
//粗体
style.Font.IsBold = false;
//应用边界线 左边界线
style.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;
//应用边界线 右边界线
style.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;
//应用边界线 上边界线
style.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;
//应用边界线 下边界线
style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;
return style;
}