导出EXCEL其他都没问题 现在是添加图片出问题。 我是一个循环添加的图片 但是最后只有最后一张图片前面的都被覆盖掉了。
private static void AddPieChart(HSSFWorkbook workbook) { string filename; int pictureIdx; HSSFPatriarch patriarch; HSSFClientAnchor anchor; Sheet sheet = workbook.GetSheet("Sheet1"); for (int i = 0; i < dt.Rows.Count; i++) { filename = dt.Rows[i][5].ToString(); byte[] bytes = System.IO.File.ReadAllBytes(filename); if (!string.IsNullOrEmpty(filename)) { pictureIdx = workbook.AddPicture(bytes, NPOI.SS.UserModel.PictureType.JPEG); patriarch = (HSSFPatriarch)sheet.CreateDrawingPatriarch(); anchor = new HSSFClientAnchor(0, 0, 100, 50, 5, i, 5 + 1, i + 1); anchor.AnchorType = 2; patriarch.CreatePicture(anchor, pictureIdx); } } }
求大神看看。 在线急等!!
没看出你的代码有啥问题。
private static void AddPieChart(HSSFWorkbook workbook)
{
string filename;
int pictureIdx;
HSSFClientAnchor anchor;
Sheet sheet = workbook.GetSheet("Sheet1");
HSSFPatriarch patriarch = (HSSFPatriarch)sheet.CreateDrawingPatriarch();
for (int i = 0; i < dt.Rows.Count; i++)
{
filename = dt.Rows[i][5].ToString();
byte[] bytes = System.IO.File.ReadAllBytes(filename);
if (!string.IsNullOrEmpty(filename))
{
pictureIdx = workbook.AddPicture(bytes, NPOI.SS.UserModel.PictureType.JPEG);
anchor = new HSSFClientAnchor(0,0, 700, 255, 5, i + 1, 5, i + 1);
anchor.AnchorType = 2;
patriarch.CreatePicture(anchor, pictureIdx);
}
}
}
对象需在开始实例化 否则后面会一直覆盖之前的对象