//調用
MemoryStream stream = OutFileToStream(NewTable(), "測試");
Workbook book = new Workbook(stream);
Worksheet sheet = book.Worksheets[0];
sheet.PageSetup.LeftMargin = 0;
sheet.PageSetup.RightMargin = 0;
sheet.PageSetup.BottomMargin = 0;
sheet.PageSetup.TopMargin = 0;
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
imgOptions.ImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg;
imgOptions.OnePagePerSheet = true;
imgOptions.PrintingPage = PrintingPageType.IgnoreBlank;
SheetRender sr = new SheetRender(sheet, imgOptions);
sr.ToImage(0, "D:\\book.jpg");
/// <summary>
/// 模擬table
/// </summary>
/// <returns></returns>
public DataTable NewTable()
{
DataTable tblDatas = new DataTable("Datas");
tblDatas.Columns.Add("ID", Type.GetType("System.Int32"));
tblDatas.Columns[0].AutoIncrement = true;
tblDatas.Columns[0].AutoIncrementSeed = 1;
tblDatas.Columns[0].AutoIncrementStep = 1;
tblDatas.Columns.Add("Product", Type.GetType("System.String"));
tblDatas.Columns.Add("Version", Type.GetType("System.String"));
tblDatas.Columns.Add("Description", Type.GetType("System.String"));
tblDatas.Rows.Add(new object[] { null, "a", "b", "c" });
tblDatas.Rows.Add(new object[] { null, "a", "b", "c" });
tblDatas.Rows.Add(new object[] { null, "a", "b", "c" });
tblDatas.Rows.Add(new object[] { null, "a", "b", "c" });
tblDatas.Rows.Add(new object[] { null, "a", "b", "c" });
return tblDatas;
}
/// <summary>
/// 寫入流
/// </summary>
/// <param name="dt"></param>
/// <param name="tableName"></param>
/// <returns></returns>
public MemoryStream OutFileToStream(DataTable dt, string tableName)
{
Workbook workbook = new Workbook(); //工作簿
Worksheet sheet = workbook.Worksheets[0]; //工作表
Cells cells = sheet.Cells; //單元格
//爲標題設置樣式
Style styleTitle = workbook.Styles[workbook.Styles.Add()]; //新增樣式
styleTitle.HorizontalAlignment = TextAlignmentType.Center; //文字居中
styleTitle.Font.Name = "宋體"; //文字字體
styleTitle.Font.Size = 14; //文字大小
styleTitle.Font.IsBold = true; //粗體
//樣式2
Style style2 = workbook.Styles[workbook.Styles.Add()]; //新增樣式
style2.HorizontalAlignment = TextAlignmentType.Center; //文字居中
style2.Font.Name = "宋體"; //文字字體
style2.Font.Size = 13; //文字大小
//style2.Font.IsBold = true; //粗體
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;
//樣式3
Style style3 = workbook.Styles[workbook.Styles.Add()]; //新增樣式
style3.HorizontalAlignment = TextAlignmentType.Center; //文字居中
style3.Font.Name = "宋體"; //文字字體
style3.Font.Size = 12; //文字大小
style3.IsTextWrapped = true; //單元格內容自動換行
style3.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;
style3.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;
style3.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;
style3.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;
int Colnum = dt.Columns.Count; //表格列數
int Rownum = dt.Rows.Count; //表格行數
//生成行1 標題行
cells.Merge(0, 0, 1, Colnum); //合併單元格
cells[0, 0].PutValue(tableName); //填寫內容
cells[0, 0].SetStyle(styleTitle);
cells.SetRowHeight(0, 38);
//生成行2 列名行
for (int i = 0; i < Colnum; i++)
{
cells[1, i].PutValue(dt.Columns[i].ColumnName);
cells[1, i].SetStyle(style2);
cells.SetRowHeight(1, 26);
cells.SetColumnWidthPixel(i, 200);
}
//生成數據行
for (int i = 0; i < Rownum; i++)
{
for (int k = 0; k < Colnum; k++)
{
cells[2 + i, k].PutValue(dt.Rows[i][k].ToString());
cells[2 + i, k].SetStyle(style3);
}
cells.SetRowHeight(2 + i, 24);
}
MemoryStream ms = workbook.SaveToStream();
return ms;
}
利用Aspose.Cells組件導出圖片
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.