且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么导出到vb.net中的excel代码不能在Server上工作,当它在本地机器上工作时。

更新时间:2023-01-10 13:08:41

您正在尝试打开Excel文件在服务器上。当您在本地运行该站点时,这是有效的,因为客户端和服务器是同一台计算机。当您将站点部署到真实服务器时,它不会起作用。您需要使用 Response.TransmitFile 将Excel文件发送到客户端。



此外,根据您的评论,您正在尝试将文件写入 C:驱动器的根目录。由于您的站点不会以管理用户身份运行,因此它无权写入该位置。选择它有权访问的文件夹 - 例如,网站中的 App_Data 文件夹。



DataSet ds = CreateSampleData();

// 构建应用程序可以写入的路径。
// 使用随机文件名来避免多个同时请求发生冲突。
string targetFolder = Server.MapPath( 〜/ App_Data / 跨度>);
string excelFilename = Path.GetRandomFileName()+ .XLSX跨度>;
string excelPath = Path.Combine(targetFolder,excelFilename);

// 创建Excel文件:
CreateExcelFile.CreateExcelDocument (ds,excelPath);

// 将文件传输到客户端:
回复.ContentType = application / vnd.openxmlformats-officedocument.spreadsheetml.sheet;
Response.AddHeader( Content-Disposition attachment; filename = Sample.xlsx);
Response.TransmitFile(excelPath);
Response.Flush();

// 删除临时文件:
File.Delete (excelPath);


http://***.com/questions/21020359/export-data-from-vb-to-excel-sheet [ ^ ]





http://***.com/questions/21020359 / export-data-from-vb-to-excel-sheet [ ^ ]



尝试thiz ..


你好,



我过去遇到过这种情况。



常见问题m在服务器中没有安装office / excel。



请尝试下载并安装以下访问引擎。



http://www.microsoft.com/en-in/download/details.aspx?id=13255



你没有提到你有哪个版本的excel。请尝试在服务器上下载并安装相应的版本。



希望这会有所帮助!



- S Francis

我的博客


I Have done Export To Excel in VS 2005 using vb.net ,My code Works Fine on local machine but i put

the Same code on my Server its Not Working .

when i click on Excel to export button , target File gives me null values on server.

my Code where the exact error throughing is like below.



Dim p As New Process
 p.StartInfo = New ProcessStartInfo(targetFilename)
 p.Start()




please Help its very important ..

You're attempting to open the Excel file on the server. When you run the site locally, that works, since the client and server are the same computer. When you deploy the site to a real server, it's not going to work. You need to send the Excel file to the client using Response.TransmitFile.

Also, based on your comment, you're trying to write the file to the root of the C: drive. Since your site won't be running as an administrative user, it won't have permission to write to that location. Pick a folder to which it does have access - for example, the App_Data folder within the site.

DataSet ds = CreateSampleData();

// Build a path to which the application can write.
// Use a random file-name to avoid collisions from multiple simultaneous requests.
string targetFolder = Server.MapPath("~/App_Data/");
string excelFilename = Path.GetRandomFileName() + ".xlsx";
string excelPath = Path.Combine(targetFolder, excelFilename);

// Create the Excel file:
CreateExcelFile.CreateExcelDocument(ds, excelPath);

// Transmit the file to the client:
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("Content-Disposition", "attachment;filename=Sample.xlsx");
Response.TransmitFile(excelPath);
Response.Flush();

// Delete the temporary file:
File.Delete(excelPath);


http://***.com/questions/21020359/export-data-from-vb-to-excel-sheet[^]


http://***.com/questions/21020359/export-data-from-vb-to-excel-sheet[^]

Try thiz..


Hi,

I have encountered these kind of situation in my past.

The common problem is in the server there is no office/excel installed.

Please try to download and install the below access engine.

http://www.microsoft.com/en-in/download/details.aspx?id=13255

You didn't mentioned which version of excel you have. Please try to download and install appropriate version in the server machine.

Hope this helps!

- S Francis
My Blog