C# 无法读取上载的excel文件
您好,我正在尝试上载一个excel文件,并将上载的excel工作表中的数据读取到另一个excel工作表中,但我收到文件不可用错误。有人能帮我解决吗C# 无法读取上载的excel文件,c#,asp.net,excel,C#,Asp.net,Excel,您好,我正在尝试上载一个excel文件,并将上载的excel工作表中的数据读取到另一个excel工作表中,但我收到文件不可用错误。有人能帮我解决吗 string filetest1; protected void UploadButton_Click(object sender, EventArgs e) { if (FileUpload1.HasFile) SaveFile(FileUpload1.PostedFile); els
string filetest1;
protected void UploadButton_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
SaveFile(FileUpload1.PostedFile);
else
UploadStatusLabel1.Text = "You did not specify a file to upload.";
}
void SaveFile(HttpPostedFile file)
{
string savePath = "c:\\Users\\*****\\Desktop\\";
// Get the name of the file to upload.
string fileName = file.FileName;
// Create the path and file name to check for duplicates.
string pathToCheck = savePath + fileName;
// Create a temporary file name to use for checking duplicates.
string tempfileName = "";
// Check to see if a file already exists with the
// same name as the file to upload.
if (System.IO.File.Exists(pathToCheck))
{
int counter = 2;
while (System.IO.File.Exists(pathToCheck))
{
// if a file with this name already exists,
// prefix the filename with a number.
tempfileName = counter.ToString() + fileName;
pathToCheck = savePath + tempfileName;
counter++;
}
fileName = tempfileName;
// Notify the user that the file name was changed.
UploadStatusLabel1.Text = "A file with the same name already exists." +
"<br />Your file was saved as " + fileName;
}
else
{
// Notify the user that the file was saved successfully.
UploadStatusLabel1.Text = "Your file was uploaded successfully.";
}
// Append the name of the file to upload to the path.
savePath += fileName;
filetest1 = fileName;
MessageBox.Show(filetest1);
// Call the SaveAs method to save the uploaded
// file to the specified directory.
file.SaveAs(savePath);
}
protected void Compare_Click(object sender, EventArgs e)
{
string filetest1 = FileUpload1.PostedFile.FileName;
**MessageBox.Show(filetest1);**
-------------
obook = oAPP.Workbooks.Open(filetest1);
}
字符串文件test1;
受保护的无效上载按钮\u单击(对象发送者,事件参数e)
{
if(FileUpload1.HasFile)
保存文件(FileUpload1.PostedFile);
其他的
UploadStatusLabel1.Text=“您没有指定要上载的文件。”;
}
无效保存文件(HttpPostedFile文件)
{
string savePath=“c:\\Users\\*****\\Desktop\\”;
//获取要上载的文件的名称。
字符串文件名=file.fileName;
//创建路径和文件名以检查重复项。
字符串路径检查=保存路径+文件名;
//创建用于检查重复项的临时文件名。
字符串tempfileName=“”;
//检查是否已存在具有的文件
//与要上载的文件同名。
if(System.IO.File.Exists(pathToCheck))
{
int计数器=2;
while(System.IO.File.Exists(pathToCheck))
{
//如果具有此名称的文件已存在,
//在文件名前面加一个数字。
tempfileName=计数器.ToString()+文件名;
pathToCheck=保存路径+临时文件名;
计数器++;
}
fileName=tempfileName;
//通知用户文件名已更改。
UploadStatusLabel1.Text=“已存在同名文件。”+
“
您的文件已另存为”+文件名;
}
其他的
{
//通知用户文件已成功保存。
UploadStatusLabel1.Text=“您的文件已成功上载。”;
}
//将要上载的文件名附加到路径。
savePath+=文件名;
filetest1=文件名;
Show(filetest1);
//调用SaveAs方法保存上传的文件
//文件到指定的目录。
file.SaveAs(savePath);
}
受保护的无效比较\u单击(对象发送方,事件参数e)
{
字符串filetest1=FileUpload1.PostedFile.FileName;
**Show(filetest1)**
-------------
obook=oAPP.Workbooks.Open(filetest1);
}
有两个消息框,第一个消息框返回上载文件的名称,但第二个消息框返回空消息框将文件名存储在会话变量中,并在需要时使用它