создать текстовый документ на сервере Windows с asp.net (C #) - PullRequest
0 голосов
/ 21 декабря 2011

У меня есть приложение asp.net, в котором я манипулирую текстовыми документами, а затем сохраняю их в папке.Приложение работает должным образом локально, но когда я запускаю то же приложение на сервере, слово документ не создается, и я получаю файл, который не может быть найден, когда я пытаюсь получить к нему доступ.Есть идеи, в чем может быть причина?Фью, у меня MS Word установлен на сервере, если это помогает.Заранее спасибо, Laziale

ОБНОВЛЕНИЕ: добавлен образец кода

Word._Application app = null;
Word.Documents docs = null;
Word.Document doc = null;
object path = HttpContext.Current.Server.MapPath(@"~\Docs\Report.doc");
Data dt = new Data();
double footprintTotalEmmissions = 0.0, totalPercentage = 0.0;

app = new Word.Application();
app.Visible = false;
app.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone;
SqlConnection conn = new SqlConnection(connString);
SqlCommand commGetUserId = new SqlCommand("GetEmailData", conn);
commGetUserId.CommandType = CommandType.StoredProcedure;
commGetUserId.CommandTimeout = 0;
try
{

    commGetUserId.Parameters.AddWithValue("@UserID", username);
    SqlDataReader rdr = null;
    conn.Open();
    rdr = commGetUserId.ExecuteReader();
    if (rdr.HasRows)
    {
        while (rdr.Read())
        {
            companyName = rdr[1].ToString();
            address = rdr[2].ToString();
            footprintEl = Convert.ToDouble(rdr[3].ToString());
            electricityConsumed = Convert.ToSingle(rdr[7].ToString());
            dollarsEl = Convert.ToDecimal(rdr[6].ToString());
            naturalGasConsumed = Convert.ToSingle(rdr[9].ToString());
            dollarsNG = Convert.ToDecimal(rdr[8].ToString());
            footprintNG = Convert.ToDouble(rdr[10].ToString());
            footprintProp = Convert.ToDouble(rdr[11].ToString());
            dollarsProp = Convert.ToDecimal(rdr[12].ToString());
            footprintheatingOil = Convert.ToDouble(rdr[13].ToString());
            dollarsHo = Convert.ToDecimal(rdr[14].ToString());
            dollarsWaste = Convert.ToDecimal(rdr[15].ToString());
            dollarsWater = Convert.ToDecimal(rdr[16].ToString());
            waterTotal = Convert.ToInt32(rdr[17].ToString());
            footprintTr = Convert.ToDouble(rdr[18].ToString());
            footprintFlights = Convert.ToDouble(rdr[19].ToString());
            toMonthYear = Convert.ToDateTime(rdr[5].ToString());
            fromMonthYear = Convert.ToDateTime(rdr[4].ToString());
        }
    }
    rdr.Close();
    //        toMonthYear = Convert.ToDateTime(docCompanyMain.SelectSingleNode("Xml/Resources/Electricity/AnnualBillingPeriod").Attributes[1].InnerText);
    int month = toMonthYear.Month;
    int yearr = toMonthYear.Year;
    totalTo = Convert.ToString(month + "/" + yearr);
    month = fromMonthYear.Month;
    yearr = fromMonthYear.Year;
    totalFrom = Convert.ToString(month + "/" + yearr);
    footprintTotalEmmissions = footprintEl + footprintNG + footprintProp + footprintheatingOil + footprintTr + footprintFlights;
    totalPercentage = footprintTotalEmmissions * 0.05;
    totalAmount = dollarsEl + dollarsHo + dollarsNG + dollarsProp + dollarsWaste + dollarsWater;
    docs = app.Documents;
    doc = docs.Open(ref path, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o);
    doc.Activate();

    foreach (Word.Range range in doc.StoryRanges)
    {
        if (range.Text.ToString().Contains("Today_Date"))
        {

            string test = "";
        }
        Word.Find find = range.Find;
        object findText = "[Company_Name]";
        object replacText = companyName;
        object findDate = "[Month_Day_Year]";
        object todaysDate = DateTime.Now.ToShortDateString();
        object findAddress = "[Company_address]";
        object total_Address = completeAddress;
        object findwaterTotal = "[Water Total]";
        object totalWater = waterTotal;
        object elecCO2Total = "[Elect_Total_Co2]";
        object co2Total = footprintEl;
        object ngCO2Total = "[NG_Total_Co2]";
        object ngTotal = footprintNG;
        object propaneTotal = "[Prop_Total_Co2]";
        object propTotal = footprintProp;
        object heatTotal = "[Heat_Oil_Total_Co2]";
        object heatingTotal = footprintheatingOil;
        object motorTotal = "[Motor_Trans_Total_Co2]";
        object motorc0total = footprintTr;
        object airMilesTotal = "[Air_Miles_Total_Co2]";
        object airCO2Total = footprintFlights;
        object totalEmmissions = "[Total_Emissions_Co2]";
        object emissionsTotal = footprintTotalEmmissions;
        object todayDate = "[Today_Date]";
        object dateToday = DateTime.Now.ToShortDateString();
        object year = "[Year]";
        object yearValue = DateTime.Now.Year;
        object compName = "[Company Name]";
        object totalCarbonFootprint = "[total_carbon_footprint]";
        object percentageSave = "[Fivepercent_total_carbon_footprint]";
        object percentageResult = totalPercentage;
        object dollarsSave = "[Dollars_saved]";
        object obDollarsSave = '$' + Convert.ToString(totalAmount);
        object fromMonthTo = "[From_Month_Year";
        object replaceFromMonthTO = totalFrom;
        object toMonthTo = "To_Month_ Year]";
        object replaceToMonthTo = totalTo;

        object replace = Word.WdReplace.wdReplaceAll;
        object findWrap = Word.WdFindWrap.wdFindContinue;

        find.Execute(ref fromMonthTo, ref o, ref o, ref o, ref oFalse, ref o,
            ref o, ref findWrap, ref o, ref replaceFromMonthTO,
            ref replace, ref o, ref o, ref o, ref o);
        find.Execute(ref toMonthTo, ref o, ref o, ref o, ref oFalse, ref o,
          ref o, ref findWrap, ref o, ref replaceToMonthTo,
          ref replace, ref o, ref o, ref o, ref o);
        find.Execute(ref findText, ref o, ref o, ref o, ref oFalse, ref o,
            ref o, ref findWrap, ref o, ref replacText,
            ref replace, ref o, ref o, ref o, ref o);
        find.Execute(ref findDate, ref o, ref o, ref o, ref oFalse, ref o,
            ref o, ref findWrap, ref o, ref todaysDate,
            ref replace, ref o, ref o, ref o, ref o);
        find.Execute(ref findAddress, ref o, ref o, ref o, ref oFalse, ref o,
            ref o, ref findWrap, ref o, ref total_Address,
            ref replace, ref o, ref o, ref o, ref o);
        find.Execute(ref findwaterTotal, ref o, ref o, ref o, ref oFalse, ref o,
            ref o, ref findWrap, ref o, ref totalWater,
            ref replace, ref o, ref o, ref o, ref o);
        find.Execute(ref elecCO2Total, ref o, ref o, ref o, ref oFalse, ref o,
            ref o, ref findWrap, ref o, ref co2Total,
            ref replace, ref o, ref o, ref o, ref o);
        find.Execute(ref ngCO2Total, ref o, ref o, ref o, ref oFalse, ref o,
            ref o, ref findWrap, ref o, ref ngTotal,
            ref replace, ref o, ref o, ref o, ref o);
        find.Execute(ref propaneTotal, ref o, ref o, ref o, ref oFalse, ref o,
            ref o, ref findWrap, ref o, ref propTotal,
            ref replace, ref o, ref o, ref o, ref o);
        find.Execute(ref heatTotal, ref o, ref o, ref o, ref oFalse, ref o,
           ref o, ref findWrap, ref o, ref heatingTotal,
           ref replace, ref o, ref o, ref o, ref o);
        find.Execute(ref motorTotal, ref o, ref o, ref o, ref oFalse, ref o,
           ref o, ref findWrap, ref o, ref motorc0total,
           ref replace, ref o, ref o, ref o, ref o);
        find.Execute(ref airMilesTotal, ref o, ref o, ref o, ref oFalse, ref o,
           ref o, ref findWrap, ref o, ref airCO2Total,
           ref replace, ref o, ref o, ref o, ref o);
        find.Execute(ref totalEmmissions, ref o, ref o, ref o, ref oFalse, ref o,
           ref o, ref findWrap, ref o, ref emissionsTotal,
           ref replace, ref o, ref o, ref o, ref o);
        find.Execute(ref year, ref o, ref o, ref o, ref oFalse, ref o,
       ref o, ref findWrap, ref o, ref yearValue,
       ref replace, ref o, ref o, ref o, ref o);
        find.Execute(ref todayDate, ref o, ref o, ref o, ref oFalse, ref o,
          ref o, ref findWrap, ref o, ref dateToday,
          ref replace, ref o, ref o, ref o, ref o);
        find.Execute(ref compName, ref o, ref o, ref o, ref oFalse, ref o,
         ref o, ref findWrap, ref o, ref replacText,
         ref replace, ref o, ref o, ref o, ref o);
        find.Execute(ref totalCarbonFootprint, ref o, ref o, ref o, ref oFalse, ref o,
       ref o, ref findWrap, ref o, ref emissionsTotal,
       ref replace, ref o, ref o, ref o, ref o);
        find.Execute(ref percentageSave, ref o, ref o, ref o, ref oFalse, ref o,
      ref o, ref findWrap, ref o, ref percentageResult,
      ref replace, ref o, ref o, ref o, ref o);
        find.Execute(ref dollarsSave, ref o, ref o, ref o, ref oFalse, ref o,
        ref o, ref findWrap, ref o, ref obDollarsSave,
        ref replace, ref o, ref o, ref o, ref o);

        Marshal.FinalReleaseComObject(find);
        Marshal.FinalReleaseComObject(range);
    }

    //  doc.Save();
    string name = "";               
    name = HttpContext.Current.Server.MapPath(@"~\Docs\Report.doc");
    object nameObject = name;
    doc.SaveAs(ref nameObject, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o);
    ((Word._Document)doc).Close(ref o, ref o, ref o);
    app.Quit(ref o, ref o, ref o);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...