使用占位符从上传的 excel 文件生成字符串 asp.net mvc 使用 EPPlus 库
Use Placeholders to generate string from uploaded excel File in asp.net mvc using EPPlus Library
我正在编写一个应用程序,用户可以在其中上传一个 excel 文件,其中包含我不知道列数的联系信息。 sheet 可以有尽可能多的列和行。我想要实现的是,用户可以通过使用基于零的索引作为 columns.Take 的占位符构建消息来向 sheet 中的每个联系人发送消息,例如 sheet下面假设第一行代表 headers
用户将像这样构建他的消息 Dear {0} {1}, your age is {2} and your email is {3}
其中 {0} 是第一列的标题,{1} 是第二列的标题,等等。这应该将消息输出为 Dear Jon Snow, your age is 27 and your email is jsnow@winterfell.com
。我想对 sheet 中的每一行执行此操作。我正在使用 EPplus 来解析 excel sheet.This 是我尝试过的。
static void Main(string[] args)
{
var path = "C:\Users\RIDWAN\Desktop\ExcelFile.xlsx";
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
using (var package = new ExcelPackage(fs))
{
var currentSheet = package.Workbook.Worksheets;//get the work sheet
var workSheet = currentSheet.First();//get the first sheet
var noOfCol = workSheet.Dimension.End.Column; //get the no of col in the active sheet
var noOfRow = workSheet.Dimension.End.Row; /get the no of row in the active sheet
var placeholder = "{FirstName}";
for (int i = 2; i <= noOfRow; i++)
{
for (int j = 1; j <= noOfCol; j++)
{
var message = String.Format("Dear {0} {1}, your age is {2} and your email is {3}", workSheet.GetValue(i, j));
Console.Write(message);
}
}
Console.ReadLine();
}
}
我想使用占位符输出格式正确的消息,以生成具有相应占位符值的消息。
似乎你只需要一个 for
循环,因为 cols 的数量是已知的 - 它的行是这里的变体。简单地做这样的事情:
using (var package = new ExcelPackage(fs))
{
var currentSheet = package.Workbook.Worksheets;//get the work sheet
var workSheet = currentSheet.First();//get the first sheet
var noOfCol = workSheet.Dimension.End.Column; //get the no of col in the active sheet
var noOfRow = workSheet.Dimension.End.Row; // get the no of row in the active sheet
var placeholder = "{FirstName}";
for (int i = 2; i <= noOfRow; i++)
{
var vals = workSheet.Cells[i, 1, i, noOfCol].Select(c => c.Value).ToArray();
var message = String.Format("Dear {0} {1}, your age is {2} and your email is {3}", vals);
Console.WriteLine(message);
}
}
给这个整数输出:
Dear Jon Snow, your age is 27 and your email is jsnow@winderfell.com
Dear Arya Stark, your age is 18 and your email is aryastark@winterfell.com
Dear Eddard Stark, your age is 50 and your email is lordeddard@winterfell.com
我正在编写一个应用程序,用户可以在其中上传一个 excel 文件,其中包含我不知道列数的联系信息。 sheet 可以有尽可能多的列和行。我想要实现的是,用户可以通过使用基于零的索引作为 columns.Take 的占位符构建消息来向 sheet 中的每个联系人发送消息,例如 sheet下面假设第一行代表 headers
用户将像这样构建他的消息 Dear {0} {1}, your age is {2} and your email is {3}
其中 {0} 是第一列的标题,{1} 是第二列的标题,等等。这应该将消息输出为 Dear Jon Snow, your age is 27 and your email is jsnow@winterfell.com
。我想对 sheet 中的每一行执行此操作。我正在使用 EPplus 来解析 excel sheet.This 是我尝试过的。
static void Main(string[] args)
{
var path = "C:\Users\RIDWAN\Desktop\ExcelFile.xlsx";
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
using (var package = new ExcelPackage(fs))
{
var currentSheet = package.Workbook.Worksheets;//get the work sheet
var workSheet = currentSheet.First();//get the first sheet
var noOfCol = workSheet.Dimension.End.Column; //get the no of col in the active sheet
var noOfRow = workSheet.Dimension.End.Row; /get the no of row in the active sheet
var placeholder = "{FirstName}";
for (int i = 2; i <= noOfRow; i++)
{
for (int j = 1; j <= noOfCol; j++)
{
var message = String.Format("Dear {0} {1}, your age is {2} and your email is {3}", workSheet.GetValue(i, j));
Console.Write(message);
}
}
Console.ReadLine();
}
}
我想使用占位符输出格式正确的消息,以生成具有相应占位符值的消息。
似乎你只需要一个 for
循环,因为 cols 的数量是已知的 - 它的行是这里的变体。简单地做这样的事情:
using (var package = new ExcelPackage(fs))
{
var currentSheet = package.Workbook.Worksheets;//get the work sheet
var workSheet = currentSheet.First();//get the first sheet
var noOfCol = workSheet.Dimension.End.Column; //get the no of col in the active sheet
var noOfRow = workSheet.Dimension.End.Row; // get the no of row in the active sheet
var placeholder = "{FirstName}";
for (int i = 2; i <= noOfRow; i++)
{
var vals = workSheet.Cells[i, 1, i, noOfCol].Select(c => c.Value).ToArray();
var message = String.Format("Dear {0} {1}, your age is {2} and your email is {3}", vals);
Console.WriteLine(message);
}
}
给这个整数输出:
Dear Jon Snow, your age is 27 and your email is jsnow@winderfell.com
Dear Arya Stark, your age is 18 and your email is aryastark@winterfell.com
Dear Eddard Stark, your age is 50 and your email is lordeddard@winterfell.com