Download arquivo excel com C sharp
08/01/2014
0
<html>
<head>
<script language="JavaScript1.2">
/**
* Get the virtual directory name (e.g. http://host/"<virtualdir>"/...)
*
* @return String The virtual directory name.
*/
function getVirtualDir()
{
var strUrl = window.location.href;
//Find the third slash (e.g. http://...''''''''/''''''''...)
var nStart = strUrl.indexOf("/");
nStart = strUrl.indexOf("/", nStart + 2) + 1;
//Find the fourth slash (after the virtual dir name)
var nEnd = strUrl.indexOf("/", nStart + 1);
return strUrl.substring(nStart, nEnd);
}
/*
* forward to the erl component
*/
function doForward() {
// get the query parameters
var strArg = "";
var strSearch = window.location.search;
if (strSearch != null && strSearch.length > 0) {
strArg = strSearch + "&";
} else {
strArg = "?";
}
strArg += "ReLoad=" + new Date().getTime();
// forward to the erl component
window.location.replace("/" + getVirtualDir() + "/component/erl" + strArg);
}
</script>
</head>
<body onload="doForward()">
</body>
</html>
Fiz um teste para fazer um download de uma imagem a partir de uma url da internet e também aconteceu a mesma coisa.
Segue abaixo os códigos que já testei:
-- Teste 1:
string url = "http://erilink.ericsson.se/eridoc/erl/objectId/09004cff86eedde6?docno=&action=current&format=excel12book";
WebClient Client = new WebClient();
Client.DownloadFile(url, @"C:\Temp\POs Manual Log.xlsx");
-- Teste 2:
string url = "http://erilink.ericsson.se/eridoc/erl/objectId/09004cff86eedde6?docno=&action=current&format=excel12book";
WebClient Client = new WebClient();
Stream strm = Client.OpenRead(url);
StreamReader sr = new StreamReader(strm);
StreamWriter sw = new StreamWriter(@"C:\temp\POs Manual Log.xlsx");
sw.Write(sr.ReadToEnd());
sw.Close();
strm.Close();
-- Teste 3:
string url = "http://erilink.ericsson.se/eridoc/erl/objectId/09004cff86eedde6?docno=&action=current&format=excel12book";
WebClient Client = new WebClient();
Stream responseStream = Client.OpenRead(url);
byte[] buffer = new byte[2048];
FileStream newFile = new FileStream(@"C:\Temp\POs Manual Log.xlsx", FileMode.Create);
int readCount = responseStream.Read(buffer, 0, buffer.Length);
while (readCount > 0)
{
//Escrever o arquivo
newFile.Write(buffer, 0, readCount);
readCount = responseStream.Read(buffer, 0, buffer.Length);
}
newFile.Close();
responseStream.Close();
Alguém pode me ajudar, por favor?
Ingrid Valentim
Posts
08/01/2014
Henrique Gasparotto
Se sim, porque esses dados estão no .xlsx e não em um .txt normal? Ou mesmo um .htm?
09/01/2014
Ingrid Valentim
09/01/2014
Leandro Chiodini
DEve ter algum problema na fonte do arquivo então.
Pois um arquivo em excel, ter o conteudo dele, como você apresentou é estranho,.
O que acontece quando um arquivo não é interpretado via Excel, é ele ficar com varios caracteres especiais.
mais nao com um codigo dentro dele.
O que pode estar acontecendo é um erro na fonte desse arquivo,
no momento em que você baixa ele para a sua maquina.
tenta entrar no excel,
Abrir ele em modo especial.
e tentar abrir ele no modelo CSV.
que é separado por ;
veja se voce consegue abrir ele dai.
como na figura.
[img:descricao=Abrir]http://arquivo.devmedia.com.br/forum/imagem/343159-20140109-140641.png[/img]
ATt,
Chiodini
09/01/2014
Ingrid Valentim
Conforme expliquei acima, o arquivo não têm problemas, pois se eu fizer o download manualmente via browser ele baixa corretamente, no entanto preciso criar uma aplicação em c# que faça download do arquivo.
09/01/2014
Leandro Chiodini
O teu Teste 1 esta correto, a principio
Mais ta falando a extensao do arquivo.
WebClient webClient = new WebClient();
webClient.DownloadFile("http://localhost/arquivo.txt", @"c:\temp\arquivo.txt");
WebClient webClient = new WebClient();
webClient.DownloadFile("http://localhost/arquivo.xls", @"c:\temp\arquivo.xls");
att
Chiodini
09/01/2014
Henrique Gasparotto
09/01/2014
Ingrid Valentim
09/01/2014
Leandro Chiodini
Você pode fazer o teste,
executando o Download do arquivo sem o c#
e veja qual o nome que ele salva,
e compara com a tua string de URL.
Se tiver diferente é o motivo que estamos comentando,
que ele ta salvando o HTML porque o caminho + nome do arquivo
nao traz resultados para o codigo.
Podes estar fazendo esse teste,
para verificar.
att,
Chiodini
09/01/2014
Leandro Chiodini
é posicionar o teu mouse no botao de download
no rodapé do navegador
vai mostrar toda a string com a url + nome do arquivo + extensão.
att
Chiodini
Clique aqui para fazer login e interagir na Comunidade :)