Asp.net 在c中使用web应用程序保存文档时出错

Asp.net 在c中使用web应用程序保存文档时出错,asp.net,ms-word,save,Asp.net,Ms Word,Save,我有一个c语言的web应用程序,它在本地机器文件夹中创建并保存一个文档。 当从我的机器上运行时,该应用程序可以正常工作-但是,当我将应用程序部署到我的web服务器时,就会出现此错误 这不是有效的文件名。请尝试以下一项或多项操作:* 检查路径以确保键入的路径正确。*选择一个文件 从文件和文件夹列表中 下面是我的源代码 //Create an instance for word app Microsoft.Office.Interop.Word.A

我有一个c语言的web应用程序,它在本地机器文件夹中创建并保存一个文档。 当从我的机器上运行时,该应用程序可以正常工作-但是,当我将应用程序部署到我的web服务器时,就会出现此错误

这不是有效的文件名。请尝试以下一项或多项操作:* 检查路径以确保键入的路径正确。*选择一个文件 从文件和文件夹列表中

下面是我的源代码

            //Create an instance for word app  
            Microsoft.Office.Interop.Word.Application winword = new Microsoft.Office.Interop.Word.Application();

            //Set animation status for word application  
            winword.ShowAnimation = false;

            //Set status for word application is to be visible or not.  
            winword.Visible = false;

            //Create a missing variable for missing value  
            object missing = System.Reflection.Missing.Value;

            //Create a new document  
            Microsoft.Office.Interop.Word.Document document = winword.Documents.Add(ref missing, ref missing, ref missing, ref missing);

            //Add header into the document  
            //foreach (Microsoft.Office.Interop.Word.Section section in document.Sections)
            //{
            //    //Get the header range and add the header details.  
            //    Microsoft.Office.Interop.Word.Range headerRange = section.Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
            //    headerRange.Fields.Add(headerRange, Microsoft.Office.Interop.Word.WdFieldType.wdFieldPage);
            //    headerRange.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
            //    headerRange.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdBlue;
            //    headerRange.Font.Size = 10;
            //    headerRange.Text = "Header text goes here";
            //}

            //Add the footers into the document  
            //foreach (Microsoft.Office.Interop.Word.Section wordSection in document.Sections)
            //{
            //    //Get the footer range and add the footer details.  
            //    Microsoft.Office.Interop.Word.Range footerRange = wordSection.Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
            //    footerRange.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdDarkRed;
            //    footerRange.Font.Size = 10;
            //    footerRange.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
            //    footerRange.Text = "Footer text goes here";
            //}

            //adding text to document  
            document.Content.SetRange(0, 0);
            document.Content.Text = "This is test document " + Environment.NewLine;

            //Add paragraph with Heading 1 style  
            Microsoft.Office.Interop.Word.Paragraph para1 = document.Content.Paragraphs.Add(ref missing);
            object styleHeading1 = "Heading 1";
            para1.Range.set_Style(ref styleHeading1);
            para1.Range.Text = "Para 1 text";
            para1.Range.InsertParagraphAfter();

            //Add paragraph with Heading 2 style  
            Microsoft.Office.Interop.Word.Paragraph para2 = document.Content.Paragraphs.Add(ref missing);
            object styleHeading2 = "Heading 2";
            para2.Range.set_Style(ref styleHeading2);
            para2.Range.Text = "Para 2 text";
            para2.Range.InsertParagraphAfter();

            //Create a 5X5 table and insert some dummy record  
            Microsoft.Office.Interop.Word.Table firstTable = document.Tables.Add(para1.Range, 5, 5, ref missing, ref missing);

            firstTable.Borders.Enable = 1;
            foreach (Row row in firstTable.Rows)
            {
                foreach (Cell cell in row.Cells)
                {
                    //Header row  
                    if (cell.RowIndex == 1)
                    {
                        cell.Range.Text = "Column " + cell.ColumnIndex.ToString();
                        cell.Range.Font.Bold = 1;
                        //other format properties goes here  
                        cell.Range.Font.Name = "verdana";
                        cell.Range.Font.Size = 10;
                        //cell.Range.Font.ColorIndex = WdColorIndex.wdGray25;                              
                        cell.Shading.BackgroundPatternColor = WdColor.wdColorGray25;
                        //Center alignment for the Header cells  
                        cell.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                        cell.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

                    }
                    //Data row  
                    else
                    {
                        cell.Range.Text = (cell.RowIndex - 2 + cell.ColumnIndex).ToString();
                    }
                }
            }

            //Save the document  
            object filename = @"c:\Data\TestSignature.doc";

            document.SaveAs(ref filename);
            document.Close(ref missing, ref missing, ref missing);
            document = null;
            winword.Quit(ref missing, ref missing, ref missing);
            winword = null;
            Label7.Text = "Document created successfully !";

您不能在服务器上的任何位置写入数据-例如,您不能使用此目录@c:\Data\TestSignature.doc;也许你可以添加权限,但最好不要

使用asp.net文件夹中的此目录写入您不想访问的数据

/App_Data/
或者在您的网站文件夹中创建其他目录并保存在那里

您可以使用HttpRuntime.AppDomainAppPath获取站点的路径,并添加额外的目录

在要写入的文件夹中,必须授予池标识的写入权限:
对不起,我的问题没有说清楚。 由创建的文档必须保存在用户/客户端计算机上。
假设所有用户都将拥有C驱动器和名为Data的文件夹。该文件夹将具有保存文件所需的权限。

感谢您的回复-让我尝试更清楚地说明这一点。@c:\Data是本地/客户端用户路径。该文档在应用程序上下载,并应保存到用户的c-drive上,并且不在服务器上。假设用户计算机上的数据文件夹可由部署is app的服务器访问。这完全不同-使用处理程序将此数据作为下载文件发送给用户,或者在您创建该文件后,还提供一个链接,以便用户可以下载它-您不能自动将其保存到您想要的用户位置-您只能将其作为下载文件提供