使用EPPlus将文本添加到Excel [英] Add Text to Excel Using EPPlus

查看:365
本文介绍了使用EPPlus将文本添加到Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个包含许多子目录的主目录。在每个子目录中都会有图像。我已设法将每个子目录中的图像导出到Excel工作簿中的每个Excel电子表格中。例如,如果我有10个子目录,那么将有1个excel工作簿和10个excel电子表格,在每个电子表格中将有每个子目录的图像。



我现在想要完成的是,如果任何子目录中没有图像,导出到Excel电子表格的子目录将为空白。我想将找不到图像添加到该空白Excel电子表格中作为文本。



这是我尝试过但没有文字出现在空白Excel工作表上:

hi all,
I have a main directory which contains many sub directories. In each sub directory there will be images. I have managed to export images from each subdirectory into each excel spreadsheet in a excel workbook. For example, if I have 10 sub directories, there will be 1 excel workbook with 10 excel spreadsheets, in each spreadsheet there will be each sub directory's images.

What I want to accomplish now is if there is no image in any sub directory, the sub directory exported to an excel spreadsheet will be blank. I want to add "No image found" to that blank excel spreadsheet as text.

This is what I have tried but no text appear on the blank excel spreadsheet:

string[] filesindirectory = Directory.GetDirectories(Server.MapPath("~/StoreImage"));  //main directory path

int ImageCount = 0;

  foreach (string subdir in filesindirectory)  //foreach sub directory in main directory
            {
                string[] splitter = subdir.Split('\\');
                string folderName = splitter[splitter.Length - 1];
                ExcelWorksheet ws = package.Workbook.Worksheets.Add("Worksheet-" + folderName); //create new worksheet
                ImageCount = 0;
                foreach (string img in Directory.GetFiles(subdir))  //foreach image in sub directory
                {
                    if (Directory.GetFiles(subdir).Length == 0)   //if no image in that sub directory
                    {
                      ws.InsertRow(5, 2);

                        // Inserting values in the 5th row
                        ws.Cells["A5"].Value = "12010";
                        ws.Cells["B5"].Value = "Drill";
                        ws.Cells["C5"].Value = 20;
                        ws.Cells["D5"].Value = 8;

                        // Inserting values in the 6th row
                        ws.Cells["A6"].Value = "12011";
                        ws.Cells["B6"].Value = "Crowbar";
                        ws.Cells["C6"].Value = 7;
                        ws.Cells["D6"].Value = 23.48;                         
                    }
                    else
                    {
                        ImageCount++;
                        System.Drawing.Image Image1 = System.Drawing.Image.FromFile(img);
                        var ADDImage1 = ws.Drawings.AddPicture("Chart" + ImageCount.ToString(), Image1);
                        Image1.Dispose();
                        // Row, RowoffsetPixel, Column, ColumnOffSetPixel
                        if (ImageCount > 1)
                        {
                            ADDImage1.SetPosition(ImageFinalPosition, 0, 2, 0);
                            ADDImage1.SetSize(770, 450);
                            ImageFinalPosition += (ImagePosition + 1); // Add 1 to have 1 row of empty row
                        }
                        else
                        {
                            ADDImage1.SetPosition(ImageCount, 0, 2, 0);
                            ADDImage1.SetSize(770, 450);
                            ImageFinalPosition = (ImageCount + ImagePosition) + 1; // Add 1 to have 1 row of empty row
                        }
                    }
                }
            }



这是我想要达到的输出:

http://i.stack.imgur.com/yi87U.png [ ^ ]



请帮帮我,谢谢!


This is the output I want to achieve:
http://i.stack.imgur.com/yi87U.png[^]

Please help me on this, thanks!

推荐答案

嗨参考以下CP文章,

使用EPPlus在C#.Net中创建/读取/编辑高级Excel 2007/2010报告
Hi refer following CP article,
Create/Read/Edit Advance Excel 2007/2010 Report in C#.Net using EPPlus


这篇关于使用EPPlus将文本添加到Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆