如何使用ASP.NET C#在服务器文件夹中创建doc文件 [英] How to create doc file in server folder using ASP.NET C#

查看:49
本文介绍了如何使用ASP.NET C#在服务器文件夹中创建doc文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨团队,



i想在服务器文件夹中创建.doc文件。



我们有一台服务器101.20.123.11在那个服务器上我们已经安装了sql server。



我的需求是我有一页填写字段并点击提交。每次点击提交,我需要在我们的服务器文件夹中创建word文件以及那些字段数据,我还需要通过database.how发送邮件作为附件这个doc文件,以提供我的服务器文件夹路径来保存doc文件。



注意:


此页面中的
我没有使用fileupload控件。



请帮忙。



我尝试过:



hi team,

i want to create .doc files in server folder.

we have one server 101.20.123.11 in that server we have installed sql server.

my requierment is i have one page in that fill the fields and click on submit .whenever click on submit ,i need to create word file along with that fields data in our server folder and also i need to send mail as attachment this doc file through database.how to give the my server folder path for saving the doc file .

note:

in this page i am not using fileupload control.

please help.

What I have tried:

protected void btnSubmit_click(object sender,EventArgs e)
   {
       try
       {
           string FilePathSave = "";
           HttpContext.Current.Response.Clear();
           HttpContext.Current.Response.Charset = "";


           HttpContext.Current.Response.ContentType = "application/msword";

           string strFileName = string.Empty;
            strFileName = "ServiceAttachApprovalDocument-" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".doc";
           HttpContext.Current.Response.AddHeader("Content-Disposition", "inline;filename=" + strFileName);

           StringBuilder strHTMLContent = new StringBuilder();

           strHTMLContent.Append("<img src='" + @"D:\CSS Projects\Inventory_VSS_04-May-2017\ImagingTOOL\images\kodac1.png'>".ToString());

          // strHTMLContent.Append("< img src = '~/images/kodac.png' visible = 'true' runat = 'server' style = 'width: 75px; height: 41px' />".ToString());
           //strHTMLContent.Append("<br>".ToString());

           strHTMLContent.Append(" <h1 title='Heading' align='Center' style='font-family:verdana;font-size:100%;background:#C0C0C0'>SERVICE ATTACH APPROVAL FORM </h1>".ToString());
           strHTMLContent.Append("<br>".ToString());
           strHTMLContent.Append("<table width='100%' border='2'>".ToString());

           //strHTMLContent.Append("<tr>".ToString());
           //strHTMLContent.Append("<td style='width: 100px;background:#99CC00'>SERVICE ATTACH APPROVAL FORM</td>".ToString());
           //strHTMLContent.Append("</tr><br/>".ToString());

           strHTMLContent.Append("<tr>".ToString());
           strHTMLContent.Append("<td colspan='2'>Name of  End User </td>".ToString());
           strHTMLContent.Append("<td>"+txtEndusername.Text+"</td>".ToString());
           strHTMLContent.Append("<td>Name of Reseller</td>".ToString());
           strHTMLContent.Append("<td>" + txtResellerName.Text + "</td>".ToString());
           strHTMLContent.Append("</tr>".ToString());

           strHTMLContent.Append("<tr>".ToString());
           strHTMLContent.Append("<td>Name of  Distributor </td>".ToString());
           strHTMLContent.Append("<td colspan='4'>" + txtDistributorName.Text + "</td>".ToString());
           strHTMLContent.Append("</tr>".ToString());

           strHTMLContent.Append("<tr>".ToString());
           strHTMLContent.Append("<td>Cat No</td>".ToString());
           strHTMLContent.Append("<td>Model </td>".ToString());
           strHTMLContent.Append("<td> Total Quantity </td>".ToString());
           strHTMLContent.Append("<td> Total Period including Standard One year </td>".ToString());
           strHTMLContent.Append("<td> Name of nearest Competitor offering Service Attach </td>".ToString());
           strHTMLContent.Append("</tr>".ToString());

           strHTMLContent.Append("<tr>".ToString());
           strHTMLContent.Append("<td>"+ddlproduct.SelectedItem.Text+"</td>".ToString());
           strHTMLContent.Append("<td>" + ddlproduct.SelectedItem.Text + "</td>".ToString());
           strHTMLContent.Append("<td> "+txtQuantity.Text+" </td>".ToString());
           strHTMLContent.Append("<td>"+txtperiodStdYear.Text+"</td>".ToString());
           strHTMLContent.Append("<td>"+txtCompetitorname.Text+"</td>".ToString());
           strHTMLContent.Append("</tr>".ToString());

           strHTMLContent.Append("<tr>".ToString());
           strHTMLContent.Append("<td colspan='2'></td>".ToString());
           strHTMLContent.Append("<td>Distributor Price(INR) </ td>".ToString());
           strHTMLContent.Append("<td> Reseller  Price(INR)</ td>".ToString());
           strHTMLContent.Append("<td> End User Price(INR) </ td>".ToString());
           strHTMLContent.Append("</tr>".ToString());

           strHTMLContent.Append("<tr>".ToString());
           strHTMLContent.Append("<td colspan='2'>Unit Product Sales Price Without Discount</td>".ToString());
           strHTMLContent.Append("<td>"+txtUPSWOutdcntDistributorprice.Text+"</ td>".ToString());
           strHTMLContent.Append("<td> " + txtUPSWOutdcntResellerPrice.Text + "</ td>".ToString());
           strHTMLContent.Append("<td>" + txtUPSWOutdcntEndUserPrice.Text + "</ td>".ToString());
           strHTMLContent.Append("</tr>".ToString());

           strHTMLContent.Append("<tr>".ToString());
           strHTMLContent.Append("<td colspan='2'>Unit Product Sales Price With Discount</td>".ToString());
           strHTMLContent.Append("<td>" + txtUPSWithdcntDistributorprice.Text + "</ td>".ToString());
           strHTMLContent.Append("<td> " + txtUPSWithdcntResellerPrice.Text + "</ td>".ToString());
           strHTMLContent.Append("<td>" + txtUPSWithdcntEndUserPrice.Text + "</ td>".ToString());
           strHTMLContent.Append("</tr>".ToString());

           strHTMLContent.Append("<tr>".ToString());
           strHTMLContent.Append("<td colspan='2'>Unit Service Attach Without Discount</td>".ToString());
           strHTMLContent.Append("<td>" + txtUSAWOutdcntDistributorprice.Text + "</ td>".ToString());
           strHTMLContent.Append("<td> " + txtUSAWOutdcntResellerPrice.Text + "</ td>".ToString());
           strHTMLContent.Append("<td>" + txtUSAWOutdcntEndUserPrice.Text + "</ td>".ToString());
           strHTMLContent.Append("</tr>".ToString());

           strHTMLContent.Append("<tr>".ToString());
           strHTMLContent.Append("<td colspan='2'>Service Attach With Discount</td>".ToString());
           strHTMLContent.Append("<td>" + txtUSAWithdcntDistributorprice.Text + "</ td>".ToString());
           strHTMLContent.Append("<td> " + txtUSAWithdcntResellerPrice.Text + "</ td>".ToString());
           strHTMLContent.Append("<td>" + txtUSAWithdcntEndUserPrice.Text + "</ td>".ToString());
           strHTMLContent.Append("</tr>".ToString());

           strHTMLContent.Append("</table>".ToString());
           strHTMLContent.Append("<br>".ToString());
           DateTime WDate = Convert.ToDateTime(System.DateTime.Now.ToShortDateString());
           string WarrentyDate = WDate.ToString("dd/MM/yyyy");
           strHTMLContent.Append(" Above prices valid for Warranty until (dd/mm/yyyy) :"+ WarrentyDate);
           strHTMLContent.Append("<br>".ToString());
           strHTMLContent.Append("<br>".ToString());
           strHTMLContent.Append("<table border='2' width='100%'>".ToString());

           strHTMLContent.Append("<tr>".ToString());
           strHTMLContent.Append("<td>Name</td>".ToString());
           strHTMLContent.Append("<td>(Sales Manager)</ td>".ToString());
           strHTMLContent.Append("<td>(Service Manager)</ td>".ToString());
           strHTMLContent.Append("</tr>".ToString());

           strHTMLContent.Append("<tr>".ToString());
           strHTMLContent.Append("<td>Signature</td>".ToString());
           strHTMLContent.Append("<td></td>".ToString());
           strHTMLContent.Append("<td></td>".ToString());
           strHTMLContent.Append("</tr>".ToString());

           strHTMLContent.Append("<tr>".ToString());
           strHTMLContent.Append("<td>Date</td>".ToString());
           strHTMLContent.Append("<td></td>".ToString());
           strHTMLContent.Append("<td></td>".ToString());
           strHTMLContent.Append("</tr>".ToString());

           strHTMLContent.Append("</table>".ToString());

           strHTMLContent.Append("<br><br>".ToString());
           strHTMLContent.Append("<p align='Center'> Note : This is dynamically generated word document </p>".ToString());


           //StreamWriter sw = File.CreateText(MapPath("myFile.txt"));
           //sw.WriteLine( txtFile.Text );
           //sw.Close();

           // Convert to string
           string strFileData = strHTMLContent.ToString();
           string strFolderValue = @"\101.20.123.11\c:\";//this is server path
          // string strFolderValue = @"D:\CSS Projects\Inventory_VSS_04-May-2017\ImagingTOOL\Word\"; //local path..
           //System.IO.
           DirectoryInfo Folder = new DirectoryInfo(strFolderValue);
           //objprop.P_SAFilename = strFileName;
           //objprop.P_SAFilepath = strFolderValue.ToString() + strFileName.ToString();
           string SAFilename = strFileName;
           string SAFilePath= strFolderValue.ToString() + strFileName.ToString();
           if (Folder.Exists)
           {
               if (strFileData != string.Empty)
               {
                   // build the path
                   FilePathSave = Folder.ToString() + strFileName;

                   // if file exist delete and then save again
                   // else save file
                   File.WriteAllText(FilePathSave, strFileData); // is supposed to overwrite the file if it is there but doesn't

               }
               else
               {
                   Response.Write("(Nothing to save)");
               }

               // If we can't find the folder
           }
           else
           {
               Response.Write("Folder [" + strFolderValue + "not found]");
           }
            BindProperties();
         int i=  objpo.InsertServiceAttachApproval(objprop, SAFilename, SAFilePath);
         if(i>0)
           {
               lblMsg.Text = "Inserted succesfully.";
               objpo.MAIL_FOR_ServiceAttchApprovalForDocFile();
               Clear();
           }

       }
       catch (Exception ex)
       {
           throw;
       }
   }

推荐答案

在服务器上,这些东西不起作用 - 除非你的托管环境为您提供这些路径,

On the server, these things do not work — unless your hosting environment provides you with these paths,
strHTMLContent.Append("<img src='" + @"D:\CSS Projects\Inventory_VSS_04-May-2017\ImagingTOOL\images\kodac1.png'>".ToString());



在那里,您需要做类似的事情,

strHTMLContent.Append("<img src='" + Server.MapPath("~/ImagingTOOL") + 
                    @"images\kodac1.png'>".ToString());



这样,检查Web应用程序文件系统根目录中的ImagingTOOL路径。因此,需要做的是检查文件驻留在服务器上的位置,然后使用 Server.MapPath()获取它们的物理路径。 ASP.NET将负责映射路径。


This would, check for the ImagingTOOL path in the root of your web app's file system. So, what needs to be done is to check where your files would reside on the server and then using Server.MapPath() fetch the physical path for them. ASP.NET would take care of mapping the paths.


这篇关于如何使用ASP.NET C#在服务器文件夹中创建doc文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆