//开始读取html文档。 StringBuilder sb = new StringBuilder(); string line; bool startread = false; using (StreamReader sr = new StreamReader(newsname,Encoding.Default)) { while (( line = sr.ReadLine()) != null) { if (startread|| true) { if (line.IndexOf("tempword.files") > -1) { line = line.Replace(this.sUserLogInId+"tempword.files/","DrawImage.aspx?UId="); } sb.Append(line+ "\r");
其他都很EASY
{
if (this.upFile.PostedFile != null)
{
if (upFile.PostedFile.ContentLength < 10)
{
Message.InnerHtml = "附件过于小";
return;
}
bool bIsExcel = false;
//Response.Write(upFile.PostedFile.ContentType.ToLower());
if (upFile.PostedFile.ContentType.ToLower()!="application/vnd.ms-excel" &&
upFile.PostedFile.ContentType.ToLower()!="application/msword"
)
{
Message.InnerHtml = "需要是Word或是Excel文档!";
return;
}
//保存上传进来的Word文档
byte[] bytefile = new byte[upFile.PostedFile.ContentLength]; string serverpath = Context.Server.MapPath(Context.Request.ApplicationPath);
string filename = Server.MapPath("UploadFiles/"+this.sUserLogInId+"tempword ");
string newsname = Server.MapPath("UploadFiles/"+this.sUserLogInId+"tempword.html");
string photopath= Server.MapPath ("UploadFiles/"+this.sUserLogInId+"tempword.files");
upFile.PostedFile.InputStream.Read(bytefile,0,upFile.PostedFile.ContentLength);
if (File.Exists(filename))
{
File.Delete(filename);
}
FileStream fs = new FileStream(filename, FileMode.CreateNew);
BinaryWriter w = new BinaryWriter(fs);
w.Write(bytefile,0,upFile.PostedFile.ContentLength);
w.Close();
fs.Close();
//保存原始的文档
object newHtmlFileName = filename+".html";
object newTextFileName = filename+".txt";
object missingValue = Type.Missing;
if(File.Exists(newHtmlFileName.ToString()))
{
File.Delete(newHtmlFileName.ToString());
}
if(File.Exists(newTextFileName.ToString()))
{
File.Delete(newTextFileName.ToString());
} if(upFile.PostedFile.ContentType.ToLower() =="application/vnd.ms-excel") //处理Excel文件
{
bIsExcel = true;
//开始Excel处理
Excel.Application appExcel = null ;
Excel.Workbook book = null;
try {
object fileName = filename;
appExcel = new Excel.Application();
book = appExcel.Workbooks.Open(fileName.ToString(),missingValue,missingValue,missingValue
,missingValue,missingValue,missingValue,missingValue,missingValue,missingValue
,missingValue,missingValue,missingValue,missingValue,missingValue);
//object saveformat = Word.WdSaveFormat.wdFormatHTML;
//object saveTextFormat = Word.WdSaveFormat.wdFormatText;
//存为HTML
book.SaveAs(newHtmlFileName,Excel.XlFileFormat.xlHtml,missingValue,missingValue,missingValue,missingValue,Excel.XlSaveAsAccessMode.xlExclusive,missingValue,missingValue,missingValue,missingValue,missingValue);
// doc.SaveAs2000(ref newfileName,ref saveformat,
// ref missingValue, ref missingValue,ref missingValue,
// ref missingValue,ref missingValue,ref missingValue,
// ref missingValue,ref missingValue,ref missingValue
// );
//存成txt 时要询问,故去掉
//book.SaveAs(newTextFileName,Excel.XlFileFormat.xlUnicodeText,missingValue,missingValue,missingValue,missingValue,Excel.XlSaveAsAccessMode.xlExclusive,missingValue,missingValue,missingValue,missingValue,missingValue);
book.Close(null,null,null);
book = null ;
//File.Delete(fileName+"a");
//存为Text
// doc.SaveAs2000(ref newTextFileName,ref saveTextFormat,
// ref missingValue, ref missingValue,ref missingValue,
// ref missingValue,ref missingValue,ref missingValue,
// ref missingValue,ref missingValue,ref missingValue
// );
// doc.Close(ref missingValue,ref missingValue,ref missingValue);
appExcel.Quit();
appExcel = null;
}
catch(Exception ex)
{
//throw(ex);
Message.InnerHtml = ex.Message;
return ;
}
finally
{
if (appExcel != null)
{
appExcel.Quit();
appExcel = null;
}
}
System.Diagnostics.Process[] ps = System.Diagnostics.Process.GetProcessesByName("EXCEL");
for(int i=0;i<ps.Length;i++)
{
if( ((TimeSpan)(DateTime.Now-ps[i].StartTime)).TotalSeconds>3) //3秒前的所有Excel进程关掉
{
//ps[i].Close();
ps[i].Kill();
}
}
}
else
{
//开始word处理
Word.Application app = null;
try {
app = new Word.Application();
Word.Document doc = null;
// C#
object fileName = filename;
doc = app.Documents.Open2000(ref fileName, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue);
object saveformat = Word.WdSaveFormat.wdFormatHTML;
object saveTextFormat = Word.WdSaveFormat.wdFormatText;
//存为HTML
doc.SaveAs2000(ref newHtmlFileName,ref saveformat,
ref missingValue, ref missingValue,ref missingValue,
ref missingValue,ref missingValue,ref missingValue,
ref missingValue,ref missingValue,ref missingValue
); //存为Text
doc.SaveAs2000(ref newTextFileName,ref saveTextFormat,
ref missingValue, ref missingValue,ref missingValue,
ref missingValue,ref missingValue,ref missingValue,
ref missingValue,ref missingValue,ref missingValue
);
doc.Close(ref missingValue,ref missingValue,ref missingValue);
}
catch(Exception ex)
{
//throw(ex);
Message.InnerHtml = ex.Message;
return ;
}
finally
{
if (app != null)
app.Quit(ref missingValue,ref missingValue,ref missingValue);
}
}
//开始读取html文档。
StringBuilder sb = new StringBuilder();
string line;
bool startread = false;
using (StreamReader sr = new StreamReader(newsname,Encoding.Default))
{
while (( line = sr.ReadLine()) != null)
{
if (startread|| true)
{
if (line.IndexOf("tempword.files") > -1)
{
line = line.Replace(this.sUserLogInId+"tempword.files/","DrawImage.aspx?UId=");
}
sb.Append(line+ "\r");
if (line == "</body>")
break;
else
continue;
}
if (line == "</head>")
{
startread = true;
}
}
sr.Close();
}
//开始读TextFile的内容
string TextContent ="";
if(!bIsExcel) //只有word读text file,Excel存成text有问题,所以从html 转换
{
StreamReader oTextReader = null;
try
{
oTextReader =new StreamReader(newTextFileName.ToString(),System.Text.Encoding.Default);
TextContent = oTextReader.ReadToEnd();
}
catch
{
CCClient.ShowErrMessage("读取文本文件时出错!",true);
return;
}
finally
{
if(oTextReader!=null)
{
oTextReader.Close();
oTextReader =null;
}
}
}
else
{
//把HTML的内容手动转换成text
TextContent = Regex.Replace(sb.ToString(),"<head>[\\s\\S]*?</head>","",RegexOptions.IgnoreCase);
TextContent = Regex.Replace(TextContent,"<[\\s\\S]*?>","",RegexOptions.IgnoreCase);
TextContent = TextContent.Trim();
}
if(File.Exists(newsname))
{
File.Delete(newsname);//delete 生成的html file
}
if(File.Exists(newTextFileName.ToString()))//delete 生成的Txt File
{
File.Delete(newTextFileName.ToString());
}
//开始上传html文档内容到数据库。
// dataSetxinwen.t_xinwen.Addt_xinwenRow(maxid,int.Parse(DropDownList1.SelectedValue),TextBox1.Text,DateTime.Now,sb.ToString(),0);
this.myConnection.Open();
SqlTransaction tran = this.myConnection.BeginTransaction();
//保存原始文件到t_Image表中
FileStream fsOrg = new FileStream(filename,FileMode.Open,FileAccess.Read);
BinaryReader brOrg = new BinaryReader(fsOrg,System.Text.Encoding.Default);
byte[] bOrg = brOrg.ReadBytes(this.upFile.PostedFile.ContentLength);
Guid orgFileId = Guid.NewGuid();
this.SaveImage(orgFileId,bOrg,-1,System.IO.Path.GetFileName(this.upFile.PostedFile.FileName),this.upFile.PostedFile.ContentType,ref myConnection,ref tran);
brOrg.Close();
fsOrg.Close();
string sCode = CCConvert.GetFilterSqlData(this.txtCode.Text.Trim());
string sName = CCConvert.GetFilterSqlData(this.txtName.Text.Trim());
string sKeyWord = CCConvert.GetFilterSqlData(this.txtKeyWords.Text.Trim());
string sAuthor = CCConvert.GetFilterSqlData(this.txtAuthor.Text.Trim());
string sNotes = CCConvert.GetFilterSqlData(this.txtNotes.Text.Trim());
string sTreeId = this.hitTreeId.Value.Trim();
string sDescription = sb.ToString(); string sCheckerId = "0"; //用这个旧的字段保存是否是Excel文件,以供下载用
if(bIsExcel)
{
sCheckerId = "1";
}
string strSQL =@" insert into xxx
(xxx,
xxx,
xxx,
xxx,
xx,
xx,
xx,
xx,
xx,
xx
)
values(
'" +sCode +"',"+
"'"+sName +"',"+
"'',"+
"'"+sNotes +"',"+
"'"+sTreeId +"',"+
"'"+sAuthor +"', "+
"'"+sKeyWord +"', "+
"1 ,"+ sCheckerId +
",'"+ orgFileId +"'"+
");select @@identity";
//SqlCommand cmd = new SqlCommand(strSQL,this.myConnection,tran);//先保存空记录
if(this.iKeyID>0)
{
strSQL ="Update xx Set xx ='" + sCode +"'," +
" xx ='" + sName +"',"+
" xx ='" + sNotes +"',"+
" xx ='" + sTreeId +"',"+
" xx ='" + sAuthor +"',"+
" xx ='" + sKeyWord +"', "+
" xx= 1, "+
" xx = "+ sCheckerId +
",xx='"+ orgFileId +"' " +
" WHERE zsk_zskid =" + this.iKeyID.ToString();
} SqlCommand cmd =null;
bool bNeedRedirect =false;
try
{
DataSet ds = DataAccess.SqlHelper.ExecuteDataset(tran,CommandType.Text,strSQL);
int iNewId =-1;
if(this.iKeyID>0)
{
iNewId = this.iKeyID;
}
else
{
if(ds.Tables[0].Rows.Count==1)
{
iNewId = Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString());
}
ds = null;
} //开始查找图片然后上传图片
if (Directory.Exists(photopath))
{
string[] dirs = Directory.GetFiles(photopath);
byte[] tmpimage;
foreach (string dir in dirs)
{
fs = new FileStream(dir,FileMode.Open,FileAccess.Read);
//FileInfo file = new FileInfo(dir);
tmpimage = new byte[fs.Length];
//fs.Read(tmpimage,0,(int)fs.Length);
BinaryReader br = new BinaryReader(fs);
br.Read(tmpimage,0,(int)fs.Length);
br.Close();
Guid ImageUniqId = Guid.NewGuid();
string ImageFileName = dir.Split('\\')[dir.Split('\\').Length-1];
sb= sb.Replace(ImageFileName,ImageUniqId.ToString(),0,sb.Length);
bool bRet = this.SaveImage(ImageUniqId,tmpimage ,iNewId,ImageFileName,"", ref this.myConnection,ref tran);
if(!bRet) //保存失败
{
fs.Close();
tran.Rollback();
return ;
}
fs.Close();
}
Directory.Delete(photopath,true);
}
//更新内容,因为文档内容可能很大,所以用这种方式保存
cmd = new SqlCommand("Update t_zsk set zsk_Description =@desc,zsk_Text=@text where zsk_zskid="+iNewId.ToString()+"",this.myConnection,tran);
SqlParameter p3 = cmd.Parameters.Add("@desc",System.Data.SqlDbType.NText);
p3.Value =sb.ToString();
SqlParameter p4 = cmd.Parameters.Add("@text",System.Data.SqlDbType.NText);
p4.Value = TextContent; cmd.ExecuteNonQuery();
tran.Commit();
bNeedRedirect = true;
//最后删除原始文件 if (File.Exists(filename))
{
File.Delete(filename);
}
}
catch(Exception ex)
{
tran.Rollback();
throw (ex);
}
finally
{
if(cmd!=null)
{
cmd.Dispose();
}
this.myConnection.Dispose();
}
if(bNeedRedirect)
{
Response.Redirect(this.sBackUrl,true);
}
}
}
然后在.........
学习 学习!!
又是 cpp2017(幕白兄) ,,上次还帮我一个忙的!!