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 Explicit
Sub 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#瀵煎嚭Word
usingSystem;
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;
}
}