NPOI 批量插入图片进Excel
2013-04-19 11:42
417 查看
staticHSSFWorkbookhssfworkbook;
#region查询
protectedvoidButton2_Click(objectsender,EventArgse)
{
if(txtDate1.Text=="")
{
Response.Write("<script>alert('开始时间不能为空!');</script>");
return;
}
if(txtDate2.Text=="")
{
Response.Write("<script>alert('结束时间不能为空!');</script>");
return;
}
if(!ShowPic())//没有数据
{
return;
}
chart1.SaveImage(this.Server.MapPath("images/StatisticsTrasfergz_Task1.jpg"),ChartImageFormat.Jpeg);
chart2.SaveImage(this.Server.MapPath("images/StatisticsTrasfergz_Task2.jpg"),ChartImageFormat.Jpeg);
ShowPic();
//chart2.Series[0]["PieLabelStyle"]="Outside";//将文字移到外侧
//chart2.Series[0]["PieLineColor"]="Black";//绘制黑色的连线。
//chart2.Series[0].Points.DataBindXY(xData,yData);
//chart2.Series[0].ChartType=SeriesChartType.Pie;
//chart2.Series[0].IsVisibleInLegend=true;
//chart2.Series[0].LegendText="#VALX#VAL个";
//chart2.Series[0].Label="#PERCENT--#VALY";
}
privateboolShowPic()
{
stringdate1=txtDate1.Text+"00:00:00";
stringdate2=txtDate2.Text+"23:59:59";
List<string>xData=newList<string>(){"待处理","已归档"};
DBdb=newDB();
stringsql="selectcount1,count2from(selectcount(1)as'count1'fromdbo.Trasfergz_Taskwherebtimebetween'"+date1+"'and'"+date2+"'andstatein('已归档'))t1,"
+"(selectcount(1)ascount2fromdbo.Trasfergz_Taskwherebtimebetween'"+date1+"'and'"+date2+"'andstatenotin('已归档','已作废'))t2";
DataTabledt=db.getTable(sql);
List<int>yData=newList<int>();//待处理的总数、归档的总数
List<int>yData2=newList<int>();//待处理的总数、归档的百分百
if(dt.Rows.Count==0)//没有数据
{
Response.Write("<script>alert('该时间段没有数据!');</script>");
returnfalse;
}
yData.Add(int.Parse(dt.Rows[0]["count1"].ToString()));
yData.Add(int.Parse(dt.Rows[0]["count2"].ToString()));
doublecount=double.Parse(dt.Rows[0]["count1"].ToString())+double.Parse(dt.Rows[0]["count2"].ToString());
doubleper1=double.Parse(dt.Rows[0]["count1"].ToString())/count;
doubleper2=double.Parse(dt.Rows[0]["count2"].ToString())/count;
intb1=int.Parse((Math.Round(per1,2)*100).ToString());//保留两位小数
intb2=int.Parse((Math.Round(per2,2)*100).ToString());//保留两位小数
yData2.Add(b1);
yData2.Add(b2);
btnOut.Visible=true;
chart1.Visible=true;
chart2.Visible=true;
chart1.Series[0]["PieLabelStyle"]="Outside";//将文字移到外侧
chart1.Series[0]["PieLineColor"]="Black";//绘制黑色的连线。
chart1.Series[0].Points.DataBindXY(xData,yData);
chart1.Series[0].ChartType=SeriesChartType.Pie;
chart1.Series[0].IsVisibleInLegend=true;
chart1.Series[0].LegendText="#VALX#VAL个";//显示单位
//chart1.Series[0].Label="#PERCENT--#VALY";//显示百分百
chart2.Series[0]["PieLabelStyle"]="Outside";//将文字移到外侧
//chart2.Series[0]["PieLabelStyle"]="Inside";//将文字移到内侧
chart2.Series[0]["PieLineColor"]="Black";//绘制黑色的连线。
chart2.Series[0].Points.DataBindXY(xData,yData2);
chart2.Series[0].ChartType=SeriesChartType.Pie;
chart2.Series[0].IsVisibleInLegend=true;
chart2.Series[0].LegendText="#VALX#VAL%";
//chart2.Series[0].Label="#VAL";
//chart2.Series[0].IsVisibleInLegend=true;
//chart2.Series[0].Label="#PERCENT--#VALY";
returntrue;
}
#endregion
#region导出数据
protectedvoidbtnOut_Click(objectsender,EventArgse)
{
////保存图片
//chart1.SaveImage(this.Server.MapPath("images/TongJiGL.jpg"),ChartImageFormat.Jpeg);
Button2.Enabled=false;
InitializeWorkbook();
ISheetsheet1=hssfworkbook.CreateSheet("PictureSheet");//创建Excel中表单名称
HSSFPatriarchpatriarch=(HSSFPatriarch)sheet1.CreateDrawingPatriarch();
//createtheanchor
HSSFClientAnchoranchor;
anchor=newHSSFClientAnchor(500,200,0,0,2,3,10,18);//最后两个参数调节图片在Excel中大小.3:图片首部的行数,18图片底部的行数,2表示图片离最左边的行数
anchor.AnchorType=2;
HSSFPicturepicture=(HSSFPicture)patriarch.CreatePicture(anchor,LoadImage(this.Server.MapPath("images/StatisticsTrasfergz_Task1.jpg"),hssfworkbook));
anchor=newHSSFClientAnchor(500,200,0,0,2,25,10,41);//图片的起始为25行,结束为41行。2表示离最左边2行
anchor.AnchorType=2;
//loadthepictureandgetthepictureindexintheworkbook
HSSFPicturepicture2=(HSSFPicture)patriarch.CreatePicture(anchor,LoadImage(this.Server.MapPath("images/StatisticsTrasfergz_Task2.jpg"),hssfworkbook));
//picture.Resize();//保存图片原始大小
//picture2.Resize();
//Resettheimagetotheoriginalsize.
//picture.Resize();//Note:Resizewillresetclientanchoryouset.
picture.LineStyle=LineStyle.DashDotGel;
picture2.LineStyle=LineStyle.DashDotGel;
WriteToFile();
ShowPic();
Button2.Enabled=true;
}
#endregion
voidWriteToFile()
{
//Writethestreamdataofworkbooktotherootdirectory
try
{
FileStreamfile=newFileStream(@"C:\Users\Administrator\Desktop\StatisticsTrasfergz_Task.xls",FileMode.Create);
hssfworkbook.Write(file);
file.Close();
}
catch(Exceptionex)
{
Response.Write("<script>alert('"+ex.Message+"')</script>");
}
}
staticvoidInitializeWorkbook()
{
hssfworkbook=newHSSFWorkbook();
//createaentryofDocumentSummaryInformation
DocumentSummaryInformationdsi=PropertySetFactory.CreateDocumentSummaryInformation();
dsi.Company="NPOITeam";
hssfworkbook.DocumentSummaryInformation=dsi;
//createaentryofSummaryInformation
SummaryInformationsi=PropertySetFactory.CreateSummaryInformation();
si.Subject="NPOISDKExample";
hssfworkbook.SummaryInformation=si;
}
publicstaticintLoadImage(stringpath,HSSFWorkbookwb)
{
FileStreamfile=newFileStream(path,FileMode.Open,FileAccess.Read);
byte[]buffer=newbyte[file.Length];
file.Read(buffer,0,(int)file.Length);
returnwb.AddPicture(buffer,PictureType.JPEG);
}
相关文章推荐
- Excel单元格内插入图片(VBA创建函数,根据图片名称批量插入)
- 2.4.5 用NPOI操作EXCEL--插入图片
- Java将png、jpg、bmp等格式图片批量插入到Excel单个单元格内
- 使用Npoi向Excel中插入图片
- npoi导出excel(模板)插入图片,文本只读,不可以修改
- C#调用NPOI组件读取excel表格数据转为datatable写入word表格中并向word中插入图片/文字/书签 获得书签列表
- NPOI插入图片到excel指定单元格
- NPOI(2.1.3)向excel中插入图片,xls文档图片插入成功,xlsx文档图片插入失败
- C#调用NPOI组件读取excel表格数据转为datatable写入word表格中并向word中插入图片/文字/书签 获得书签列表
- 2.4.5 用NPOI操作EXCEL--插入图片
- excel 插入图片 NPOI
- asp.net两种方式在模板excel中插入图片:npoi和c# com组件
- NPOI2.2.0.0实例详解(十一)—向EXCEL插入图片
- NPOI向Excel文件插入图片
- 2.4.5 用NPOI操作EXCEL--插入图片
- Excel中批量插入图片并对齐
- NPOI2.2.0.0实例详解(十一)—向EXCEL插入图片
- NPOI导出excel,插入图片
- c# 应用NPOI 获取Excel中的图片,保存至本地的算法
- Excel中无法插入和黏贴图片的解决方法