您的位置:首页 > 其它

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);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: