Asp.net 如何从excel文件中获取特定列的数据?
嗨,我有一个excel文件,看起来像这样 id名称年龄phne否,像这样我有35列 我正在使用一个Web应用程序,其中有一个文件上载控件、一个按钮和两个文本框Asp.net 如何从excel文件中获取特定列的数据?,asp.net,c#-4.0,Asp.net,C# 4.0,嗨,我有一个excel文件,看起来像这样 id名称年龄phne否,像这样我有35列 我正在使用一个Web应用程序,其中有一个文件上载控件、一个按钮和两个文本框 fileuploadcontrol button textbox1 textbox2 现在,当我上传excel文件的按钮点击它应该读完整的文件。。。。。。。。。 当我在textbox1中输入所需的列时,我只得到这些列的详细信息,如下所示 textbox1c1、c4、c5
fileuploadcontrol
button
textbox1
textbox2
现在,当我上传excel文件的按钮点击它应该读完整的文件。。。。。。。。。
当我在textbox1中输入所需的列时,我只得到这些列的详细信息,如下所示
textbox1c1、c4、c5、c30我只需要获取这些列的详细信息,在另一个文本框中,如果我输入要保存的位置,它应该保存在该位置。在上传和读取文件之前,任何人都可以帮助我完成此操作……我所需要的是如何评估这些文本框以获取我所需的数据
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
if (FileUpload1.PostedFile.ContentType == "application/xlsx")
{
path = Server.MapPath(".") + "\\inputfiles\\" + Guid.NewGuid() + FileUpload1.FileName;
FileUpload1.PostedFile.SaveAs(path);
Label1.Text = "File Uploaded Successfully...";
StreamReader reader = new StreamReader(FileUpload1.FileContent);
string text = reader.ReadToEnd();
}
else
Label1.Text = "Upload .xlsx File";
}
else
Label1.Text = "Upload file";
}
选中此项:
您可以指定在select查询中需要读取的列
关于我自己得到了答案……,希望在任何情况下,如果有任何需要,我都能帮助我发布我的代码
protected void btns_Click(object sender, EventArgs e)
{
string path = string.Empty;
location = Textbox2.Text;
if (fup1.HasFile)
{
try
{
path = Server.MapPath(".") + "\\uploadedfiles\\" + Guid.NewGuid() + fup1.FileName;
fup1.PostedFile.SaveAs(path);
lbl5.Text = "Upload status: File uploaded!";
}
catch (Exception ex)
{
lbl5.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
}
excelfile(path);
}
}
void excelfile( string path)
{
string readfile = path;
// initialize the Excel Application class
Excel.Application app = new Excel.Application();
//Excel.Worksheet NwSheet;
Excel.Range ShtRange;
// create the workbook object by opening the excel file.
Excel.Workbook workBook = app.Workbooks.Open(readfile, 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
// Get The Active Worksheet Using Sheet Name Or Active Sheet
Excel.Worksheet workSheet = (Excel.Worksheet)workBook.ActiveSheet;
int index = 1;
System.Text.StringBuilder sb = new StringBuilder();
try
{
string[] values = Textbox1.Text.Split(new char[] { ',' });
int[] colindexs = new int[values.Length];
for (int i = 0; i < values.Length; i++)
{
colindexs[i] = Convert.ToInt16(values[i]);
}
while (((Excel.Range)workSheet.Cells[index, 1]).Value2 != null)
{
string str = "";
foreach (int col1 in colindexs)
{
str = str + Convert.ToString(((Excel.Range)workSheet.Cells[index, col1]).Value2) + ",";
}
str = str.Substring(0, str.Length - 1);
sb.Append(str);
sb.Append(Environment.NewLine);
index++;
}
Writetofile(sb.ToString());
ShtRange = workSheet.UsedRange;
}
catch (Exception ex)
{
app.Quit();
lbl5.Text=ex.Message;
}
}
void Writetofile(string content)
{
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(location, true))
{
sw.Write(content);
sw.Flush();
}
}
}
我有一点想法,但是如何使用textbox…我应该获得在textbox中输入的列的数据。你能给出一个具体的例子,或者干脆把它写在你的帖子上。示例:输入textbox=c2,col3,col4,然后显示一些内容..看,我有一个excel文件,其中有35列我已将该文件上载到服务器并读取了该文件,现在我不需要用户输入所需列并在其他文件中获取数据的所有列..我是否应该将任何按钮置于textbox中