我有一个问题:我需要创建一个web服务(在C#中),可以从JavaScript调用,返回一个文件,这是一个Excel中生成的,并为他的返回,在JavaScript中,打开一个新窗口与此文件。 已经创建了从javascript调用的Web服务。问题是,首先要返回这个文件,第二个问题是JavaScript获得(可能不需要管理将该文件记录到光盘),然后在新窗口中打开它。Web服务返回一个文件用JavaScript打开。行动!
我经过几天的搜索,唯一的解决方案是返回一个文件就像是一个字节数组,它甚至不能将它识别为文件,远远不能在另一个窗口中打开它。我已经找到了跨ActiveX的文件,用javascript打开光盘(.txt)中的文件,但它不好,因为它首先必须接收文件并记录它,它不想做的事情,但尽管它想要,但我不知道是否有可能做到。
我很感谢你提前给予的一切帮助。 问候。
要延长,这是代码: 的javascript:
<script type="text/javascript">
function exportar(ctrl) {
var txt = ctrl.innerHTML;
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
xmlHttp.open("post", "http://localhost:65380/HtmlToExcel.asmx/ExportarExcel", true);
xmlHttp.onreadystatechange=doUpdate;
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttp.send("paginaHtml="+txt);
}
function doUpdate() {
if(xmlHttp.readyState==4)
{
var xmlDoc=xmlHttp.responseXML;
var resp = xmlHttp.responseText;
var responseElement=xmlDoc.getElementsByTagName("string")[0];
var respText=responseElement.firstChild.nodeValue;
// Open the window:
var manej = window.open(respText);
alert(manej);
manej.document.body.innerHTML = respText;
}
}
</script>
web服务:
public string ExportarExcel(string paginaHtml)
{
//return "<% Response.ContentType = \"application/vnd.ms-excel\" %>" + paginaHtml;
// Generamos un objeto excel Aplicación:
Microsoft.Office.Interop.Excel.Application Aplic = new Microsoft.Office.Interop.Excel.Application();
Aplic.Visible = false;
Microsoft.Office.Interop.Excel.Workbook libro = Aplic.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
Microsoft.Office.Interop.Excel.Worksheet hoja = (Microsoft.Office.Interop.Excel.Worksheet)libro.Worksheets[1];
if (hoja == null)
{
return null;
}
hoja.Rows.Clear();
// Chequeamos lo que nos viene y lo cargamos en el excel:
int i = 0;
int m = 1; int n = 1;
paginaHtml = paginaHtml.Replace('\r', ' ').Replace('\n', ' ');
paginaHtml = paginaHtml.Replace("\\r", " ").Replace("\\n", " ");
paginaHtml = paginaHtml.Replace(" ", " ");
paginaHtml = paginaHtml.ToLower().Replace("<b>", "").Replace("</b>", "");
paginaHtml = paginaHtml.Replace("><", "> <");
string[] palabra = paginaHtml.Split(' ');
while (i < palabra.Length)
{
// Code to convert html table to excel file.
}
string fichero = "HtmlToExcel-" + DateTime.Now.ToFileTime() + ".xls";
string ruta = (string)Server.MapPath("~") + fichero;
hoja.SaveAs(ruta, Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel7, Type.Missing, Type.Missing, false, false, false, Type.Missing, Type.Missing, Type.Missing);
// Cerramos el fichero:
libro.Close(true, Type.Missing, Type.Missing); // Cierra el libro con cbios.
hoja = null;
Aplic.Quit();
Aplic = null;
//// Transformamos a byte[]:
byte[] fExcel = ConvertirFileToByteArray(ruta);
// Borramos Fichero:
File.Delete(ruta);
return Encoding.Unicode.GetString(fExcel);
}
public static byte[] ConvertirFileToByteArray(string ruta)
{
FileStream fs = new FileStream(ruta, FileMode.Open, FileAccess.Read);
/*Create a byte array of file stream length*/
byte[] b = new byte[fs.Length];
/*Read block of bytes from stream into the byte array*/
fs.Read(b, 0, System.Convert.ToInt32(fs.Length));
/*Close the File Stream*/
fs.Close();
return b;
}
我有一页的innerHTML的Excel来变换为página和后来在新窗口显示此页。 你能举个例子吗?
你能否缓解你的问题?无论如何,我的理解是,您需要创建某种表格数据,并在调用Web服务时在Excel中打开它。那是你要的吗 ? – nepsdotin 2010-12-16 10:04:18