net输出文本文件|在vbnet中如何生成一个txt文件并且写入一个数组的值

A. 关于ASP.net 向客户端输出文件的问题

这是我做过的一个下载页面,不会另打开一个页面,也不会刷新,它用Response.Write直接向前台输出二进制。源文件代码在这里,你应该看得懂吧。需要的命名空间,请你在VS的智能提示下补充上去。string strPhyPath = Server.MapPath("~/")+ fileurl;//fileurl是文件的相对地址if (File.Exists(strPhyPath)){ //取文件大小 FileStream MyFileStream; uint FileSize1; MyFileStream = new FileStream(strPhyPath, FileMode.Open, FileAccess.Read, FileShare.None); int iConverTemp = Convert.ToInt32(MyFileStream.Length); FileSize1 = (uint)(iConverTemp); MyFileStream.Close(); //存在,下载 Page.Response.ContentType = "APPLICATION/OCTET-STREAM"; Page.Response.AddHeader("Content-length", FileSize1.ToString());//下载文件长度 Page.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(strPhyPath, System.Text.Encoding.UTF8)); Page.Response.WriteFile(strPhyPath); Response.Flush(); Response.End();}else{ this.Alert("文件不存在,请及时向管理员反馈!");//Alert是我自定义定义的方法}

B. 在vb.net中如何生成一个txt文件,并且写入一个数组的值

已测试,代码如下:

ImportsSystem.IO'引入文本操作

PublicClassForm1

PrivateSubButton1_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton1.Click

DimtxtAsString=Me.getTxt

SaveTxt(txt)

EndSub

'取得存储的文本

PrivateFunctiongetTxt()AsString

Dimx(0To10)AsInteger

ForiAsInteger=0Tox.Length-1

x(i)=CInt(100*Rnd())

Next

DimstrAsString=""

ForjAsInteger=0Tox.Length-1

str+=x(j).ToString&vbCrLf

Next

Returnstr

EndFunction

'存储文本

PrivateSubSaveTxt(ByValvAsString)

DimfpAsString=GetSavePath()

IfString.IsNullOrEmpty(fp)=FalseThen

'StreamWriter

DimSwriterAsNewStreamWriter(fp,False)'覆盖或新建

Swriter.WriteLine(v)

Swriter.Close()

EndIf

EndSub

'取得存储路径

PrivateFunctionGetSavePath()AsString

sd.FileName=""

sd.AddExtension=True

sd.DefaultExt="txt"

sd.Filter="TXT文件(*.txt)|*.txt"

sd.ShowDialog()

Returnsd.FileName

EndFunction

EndClass

测试截图如下。

C. 我要用C#,NET 导出一个word文档。文档内容只要一个表格。

楼上的 你能不能改写下这个例子啊? 我这个只能导出字符 不能导出表格 string title = "个人信息"; object titleLengh = title.Length; string first = "\n 公司最近需要利用C#对项目进行编程,其" + "中存在一个功能就是可自动生成WORD文档,但一直以来都" + "找不到什么好办法,无奈之下,只有自已学着写一个了."; object firstLengh = first.Length; string second = "\n 如果能真正生成WORD文档的好处有:"; object secondLengh = second.Length; string third = "\n1、根据数据库信息自动生成文档;"; object thirdLengh = third.Length; string forth = "\n2、免去书写文档之苦;"; object forthLengh = forth.Length; string fifth = "\n3、可以通过邮件方式传出文档。"; object fifthLengh = fifth.Length; object missing; object zero = 0; object c = 10; object one = 1; object two = 2; object tree = 3; object four = 4; object five = 5; object six = 6; object seven = 7; object eight = 8; object nine = 9; object ten = 10; Object Nothing = System.Reflection.Missing.Value; Microsoft.Office.Interop.Word.ApplicationClass wa = new Microsoft.Office.Interop.Word.ApplicationClass(); Microsoft.Office.Interop.Word.Document WordDoc = wa.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing); missing = System.Reflection.Missing.Value; wa.Visible = true; wa.Documents.Add(ref missing, ref missing, ref missing, ref missing); Microsoft.Office.Interop.Word.Range myRange = wa.ActiveDocument.Range(ref zero, ref zero); object r = myRange; Microsoft.Office.Interop.Word.Paragraph p = wa.ActiveDocument.Paragraphs.Add(ref r); p.Range.InsertBefore(title); //p.Range.Font.Size = 1;Microsoft.Office.Interop.Word.Range titleRange = wa.ActiveDocument.Range(ref zero, ref titleLengh); //titleRange.Font.Size = 1; titleRange.Font.Name = "幼圆"; titleRange.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorBlue; //MessageBox.Show("NO.1"); //titleRange.Paragraphs.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight; Microsoft.Office.Interop.Word.Range firstR = wa.ActiveDocument.Paragraphs[1].Range;//.Item(2).Range; Microsoft.Office.Interop.Word.Table table = WordDoc.Tables.Add(titleRange, 3, 3, ref Nothing, ref Nothing); r = firstR; p = wa.ActiveDocument.Paragraphs.Add(ref r);firstR.Font.Size = 40; firstR.Paragraphs.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;//在表格第一单元格中添加自定义的文字内容 table.Cell(1, 1).Range.Text = "lllll";firstR.InsertAfter(first);//firstR.InsertParagraphAfter(); //firstR=wa.ActiveDocument.Paragraphs.Item(3).Range; firstR.InsertAfter(second); firstR.InsertAfter(third); firstR.InsertAfter(forth); firstR.InsertAfter(fifth); Context.Response.Write("成功");

D. 濡備綍鍦.net涓婃彁鍙朩ord锛孭d鐨勬枃鏈鍐呭

涓锛氬埄鐢╓ord閲岄潰鐨勨滅储寮曗濆姛鑳姐1銆侀変腑闇瑕佹彁鍙栫殑鏂囧瓧锛屾墽琛岃彍鍗曞懡浠も滄彃鍏-寮曠敤-绱㈠紩鍜岀洰褰曗濓紱2銆佸湪鈥滅储寮曗濋夐」椤甸噷闈㈤夋嫨鍗曞嚮鎸夐挳鈥滄爣璁扮储寮曢」鈥濓紱3銆佹寚瀹氣滈夐」鈥濅负鈥滃綋鍓嶉〉鈥濓紝鏍规嵁闇瑕佸彲浠ユ寚瀹氭彁鍙栦箣鍚庢樉绀虹殑椤电爜鏍煎紡涓哄姞绮楁垨鑰呭炬枩锛屽崟鍑烩滄爣璁扳濓紱4銆佹ゆ椂鏍囪扮储寮曢」绐楀彛骞朵笉浼氬叧闂锛岃屼笖浣犺繕鑳藉湪鏂囨。涓鎵惧埌骞堕変腑涓嬩竴涓闇瑕佹彁鍙栫殑鏂囧瓧锛岄噸澶嶆ラ3锛5銆佸傛灉娌℃湁鍏跺畠闇瑕佹彁鍙栫殑鏂囧瓧浜嗭紝鍗曞嚮鈥滃叧闂鈥濓紱6銆佸湪闇瑕佹彃鍏ョ储寮曡〃鐨勫湴鏂癸紝鎵ц岃彍鍗曞懡浠も滄彃鍏-寮曠敤-绱㈠紩鍜岀洰褰曗濓紝鏍规嵁闇瑕佽剧疆绱㈠紩琛ㄧ殑鏍煎紡锛堟瘮濡傚垎鏍忋佸归綈銆佺缉杩涚瓑椋庢牸锛夛紝鐒跺悗鍗曞嚮鈥滅‘瀹氣濄杩欐牱鎵鏈夋爣璁拌繃绱㈠紩鐨勬枃瀛椾互鍙婂畠浠鎵鍦ㄧ殑椤电爜灏遍兘鎻愬彇鍑烘潵浜嗐濡傛灉闇瑕佹彁鍙栫殑鏂囧瓧鏈夊緢澶氾紝閭d箞鎴栬镐篃鍙浠ョ敤VBA瀹忔潵鑷鍔ㄦ爣璁扮储寮曪紙鍗崇浉褰撲簬鑷鍔ㄥ畬鎴愪笂闈㈢殑1-5姝ラわ級銆備絾鏄鍓嶆彁鏄杩欎簺鏂囧瓧蹇呴』閮芥湁闈炲父鐗瑰畾鐨勬牸寮忥紝鍗冲彲浠ヤ繚璇佸綋鍓嶆枃妗d腑鍑℃槸鍏锋湁杩欎釜鏍煎紡鐨勬枃鏈锛岄兘鏄闇瑕佹彁鍙栫储寮曠殑鏂囨湰銆浜岋細涓嬪垪VBA浠g爜鍙浠ヨ嚜鍔ㄤ负褰撳墠鏂囨。涓鐨勬墍鏈夌孩鑹叉枃瀛楀缓绔嬬储寮曟潯鐩锛屽苟鍦ㄥ綋鍓嶅厜鏍囦綅缃澶勬彃鍏ュ甫鏂囧瓧鏉$洰浠ュ強瀵瑰簲椤电爜鐨勭储寮曡〃锛Option ExplicitSub AutoMarkIndexForAllRedText() Dim oSaveSelection As Range Set oSaveSelection = Selection.Range Selection.StartOf wdStory Selection.Find.ClearFormatting Selection.Find.Font.Color = wdColorRed ' 鍦ㄨ繖閲屽彲浠ヨ剧疆鏇村氱殑鍖归厤鏍煎紡锛屽傚瓧浣撱佸瓧鍙风瓑 While Selection.Find.Execute(FindText:="", Forward:=True, Format:=True) ActiveDocument.Indexes.MarkEntry(Selection.Range, Selection.Range.Text).Select Selection.Collapse wdCollapseEnd Wend oSaveSelection.Select ActiveDocument.Indexes.Add Selection.Range MsgBox "瀹屾垚锛"End Sub濡傛灉妤间富瀵逛簬杩欎釜浠g爜鐢熸垚鐨勭储寮曡〃鏍峰紡涓嶅お婊℃剰锛屼篃娌″叧绯汇傚洜涓虹幇鍦ㄦ墍鏈夌储寮曟潯鐩宸茬粡寤哄ソ锛屾墍浠ュ彧闇鎶婅嚜鍔ㄧ敓鎴愮殑绱㈠紩琛ㄥ垹闄ゅ悗鐢╓ord鑷宸辩殑鈥滄彃鍏-寮曠敤-绱㈠紩鍜岀洰褰曗濆懡浠ゆ潵閲嶆柊鐢熸垚绗﹀悎鑷宸辫佹眰鏍峰紡鐨勭储寮曡〃鍗冲彲銆

E. .net winfrom绋嬪簭鎬庝箞鎶婁竴涓椤甸潰瀵煎嚭瀛樹负word鏂囨。

//C#瀵煎嚭WordusingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingWord=Microsoft.Office.Interop.Word;usingSystem.Reflection;usingSystem.Data;namespaceExportTest{///<summary>///瀵煎嚭word///</summary>publicclassExportWord{///<summary>///灏嗘暟鎹琛ㄥ煎嚭鍒皐ord///</summary>///<paramname="dt">鏁版嵁琛</param>///<paramname="isShowWord">鏄鍚︽樉绀簑ord</param>///<paramname="fileName">瀵煎嚭鍒版枃浠跺悕</param>///<paramname="picName">瀵煎嚭鍒皐ord涓鍥剧墖鍚</param>///<returns></returns>publicstaticboolExportToWord(DataTabledt,boolisShowWord,objectfileName,stringpicName){if(System.IO.File.Exists(fileName.ToString())){System.IO.File.Delete(fileName.ToString());}boolresult=true;Objectmyobj=Missing.Value;;if(dt==null||dt.Rows.Count==0){result=false;}else{//寤虹珛Word搴旂敤Word.Applicationword=newWord.Application();//word鏂囨。Word.Documentmydoc=newWord.Document();mydoc=word.Documents.Add(refmyobj,refmyobj,refmyobj,refmyobj);word.Visible=isShowWord;mydoc.Select();Word.Selectionmysel=word.Selection;//mysel.InlineShapes.AddPicture(picName,refmyobj,refmyobj,refmyobj);objecttop=100;objectleft=300;objecthw=100;//灏嗘暟鎹鐢熸垚Word琛ㄦ牸鏂囦欢Word.Tablemytable=mydoc.Tables.Add(mysel.Range,dt.Rows.Count,dt.Columns.Count,refmyobj,refmyobj);//璁剧疆鍒楀mytable.Columns.SetWidth(50,Word.WdRulerStyle.wdAdjustNone);//杈撳嚭鍒楁爣棰樻暟鎹for(inti=0;i<dt.Columns.Count;i++){mytable.Cell(1,i+1).Range.InsertAfter(dt.Columns[i].ColumnName);}//杈撳嚭鎺т欢涓鐨勮板綍for(inti=0;i<dt.Rows.Count-1;i++){for(intj=0;j<dt.Columns.Count;j++){mytable.Cell(i+2,j+1).Range.InsertAfter(dt.Rows[i][j].ToString());}}mydoc.Shapes.AddPicture(picName,refmyobj,refmyobj,refleft,reftop,refhw,refhw,refmyobj);//淇濆瓨鏂囨湰mydoc.SaveAs(reffileName,refmyobj,refmyobj,refmyobj,refmyobj,refmyobj,refmyobj,refmyobj,refmyobj,refmyobj,refmyobj,refmyobj,refmyobj,refmyobj,refmyobj,refmyobj);}returnresult;}}


赞 (0)