1

ボタンをクリックすると、asp.net で Excel レポートと html レポートを作成し、アプリケーションで作成してクライアント デスクトップに保存しますが、Excel ファイルがサーバーのデスクトップで作成されているため、正しく機能しません。この問題を解決するにはどうすればよいですか?

お返事ありがとうございます。

string CurrentDate;
DateTime saveNow = DateTime.Now;
CurrentDate = saveNow.Date.ToShortDateString();
string reportContent = prepareHTM();

string pathFile = Environment.GetFolderPath(Environment.SpecialFolder.CommonDesktopDirectory) + "\\As_Build_Report_ "+ CurrentDate + ".html";

using (StreamWriter outfile = new StreamWriter(pathFile, true))
{
    outfile.WriteLine(reportContent);
}
System.Diagnostics.Process.Start(pathFile);

object missing = System.Reflection.Missing.Value;
//Start Excel Application.
Excel.Application oXL = new Excel.Application();
oXL.Visible = true; //display Application .            
Excel._Workbook oWB = (Excel._Workbook)(oXL.Workbooks.Add(missing));//create a new workbook.
Excel._Worksheet oSheet = (Excel._Worksheet)oWB.ActiveSheet; //create a sheet                

string CurrentDate;
DateTime saveNow = DateTime.Now;
CurrentDate = saveNow.Date.ToString();
int keep = 5;
string project = list[0].Project;
oSheet.Cells[1, 3] = "MiKES Configuration Management __" + project + "__ As-Built Report";
oSheet.Cells[3, 1] = "Report Date :";
oSheet.Cells[3, 2] = CurrentDate;

oSheet.Cells[keep, 1] = "PART NO";
oSheet.Cells[keep, 2] = "REF.DES.";
oSheet.Cells[keep, 3] = "DESCRIPTION";
oSheet.Cells[keep, 4] = "SERIAL NO";
oSheet.Cells[keep, 5] = "C/S";
oSheet.Cells[keep, 6] = "D/C";
oSheet.Cells[keep, 7] = "REMARK";
keep++;
foreach(Classes.CMNewPart item in list)
{
    try
    {
        oSheet.Cells[keep, 1] = item.PartNo;
        oSheet.Cells[keep, 2] = item.RefDes;
        oSheet.Cells[keep, 3] = item.Description1;
        oSheet.Cells[keep, 4] = item.SerialNo;
        oSheet.Cells[keep, 5] = item.Cs;
        oSheet.Cells[keep, 6] = item.Dc;
        oSheet.Cells[keep, 7] = item.Remark;
    }
    catch (Exception)
    {

    }
    keep++;
}
4

4 に答える 4

3

Responseオブジェクトを使用してファイルをクライアントに送信する必要があります。クライアントがExcelファイルを開いているときのような警告メッセージを無視するには-

ここに画像の説明を入力してください

これを防ぐには、応答でコンテンツの種類と長さを指定する必要があります。サンプルコードを使用してください

//Read the Excel file in a byte array. here pck is the Excelworkbook              
Byte[] fileBytes = pck.GetAsByteArray();

//Clear the response               
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.Cookies.Clear();
//Add the header & other information      
Response.Cache.SetCacheability(HttpCacheability.Private);
Response.CacheControl = "private";
Response.Charset = System.Text.UTF8Encoding.UTF8.WebName;
Response.ContentEncoding = System.Text.UTF8Encoding.UTF8;
Response.AppendHeader("Content-Length", fileBytes.Length.ToString());
Response.AppendHeader("Pragma", "cache");
Response.AppendHeader("Expires", "60");
Response.AppendHeader("Content-Disposition",
"attachment; " +
"filename=\"ExcelReport.xlsx\"; " +
"size=" + fileBytes.Length.ToString() + "; " +
"creation-date=" + DateTime.Now.ToString("R") + "; " +
"modification-date=" + DateTime.Now.ToString("R") + "; " +
"read-date=" + DateTime.Now.ToString("R"));
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
//Write it back to the client    
Response.BinaryWrite(fileBytes);
Response.End();
于 2012-06-11T05:55:19.357 に答える
2

応答のHttpResponse.WriteFile メソッドに Excel ファイルを書き込むことができます。

string CurrentDate;
DateTime saveNow = DateTime.Now;
CurrentDate = saveNow.Date.ToShortDateString();
string reportContent = prepareHTM();

string pathFile = Environment.GetFolderPath(Environment.SpecialFolder.CommonDesktopDirectory) + "\\As_Build_Report_ "+ CurrentDate + ".html";

using (StreamWriter outfile = new StreamWriter(pathFile, true))
{
    outfile.WriteLine(reportContent);
}

System.IO.FileInfo file = new System.IO.FileInfo(pathFile); 
Response.Clear(); 
Response.Charset="UTF-8"; 
Response.ContentEncoding=System.Text.Encoding.UTF8; 
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name); 
Response.AddHeader("Content-Length", file.Length.ToString());    
Response.ContentType = "application/ms-excel";  
Response.WriteFile(file.FullName); 
Response.End(); 
于 2012-06-11T06:14:12.933 に答える
1

選択した最良の回答が機能するように注意する重要な点の 1 つ
は、質問で暗示されている Microsoft.Office.Interop.Excel とは異なるライブラリを使用する必要があることです。EPPlus を使用する必要があります。これは、プロジェクトで設定する方法です。

  1. Visual Studio のパッケージ マネージャー コンソールから、次のように入力します。Install-Package EPPlus (これにより、必要に応じてライブラリと参照がインストールされます)
  2. これを使用してステートメントを追加します ( using OfficeOpenXml;)

そして、これは、最良の回答で言及されているように、Excel パッケージ (pck) を作成するためのサンプル コードです。

    ExcelPackage pck = new ExcelPackage();
    ExcelWorksheet ws = pck.Workbook.Worksheets.Add("exported data");
    // Excel sheet headings
    ws.Cells[1, 1].Value = "Column 1 title";
    ws.Cells[1, 2].Value = "Column 2 title";


これで、最良の回答で提供されたコードを使用できます。

于 2016-02-26T23:43:45.300 に答える
0

特定のクライアント側の場所に直接保存することはできません。あなたができることは、ブラウザ側で「ファイルの保存」ダイアログがポップアップするように、その要求でファイルを返すことです。

于 2012-06-11T05:51:44.160 に答える