using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using DevComponents.DotNetBar; using System.Data.OracleClient; using System.IO; using System.Configuration; namespace Cyberpipe { public partial class FormDocumentManager : Office2007Form { private int pagesize = 10; private int currentpage = 1; private int lastpage = 1; public static bool IS_OPEN = false; public FormDocumentManager() { InitializeComponent(); } public void reloadGrid(int pageIndex) { if (pageIndex <= 0) { pageIndex = 1; currentpage = 1; } String sqlrows = "select * from (select rownum as rowno,dbid,filename,title,filetype,upday,writer,'删除' as btnDel,'预览' as btnLook,'下载' as down from casic_userdocument where sysname='EMS' "; string sqlcount = "select count(*) from casic_userdocument where sysname='EMS' "; if (!String.IsNullOrEmpty(txt_title.Text.Trim())) { sqlrows += " and title like '%" + txt_title.Text.Trim() + "%'"; sqlcount += " and title like '%" + txt_title.Text.Trim() + "%'"; } if (dateTime_begin.Value != null) { sqlrows += " and upday>=to_date('" + dateTime_begin.Value.ToShortDateString() + "','yyyy-MM-dd')"; sqlcount += " and upday>=to_date('" + dateTime_begin.Value.ToShortDateString() + "','yyyy-MM-dd')"; } if (dateTime_end.Value != null) { DateTime time = dateTime_end.Value; time.AddDays(1); sqlrows += " and upday<=to_date('" + time.ToShortDateString() + "','yyyy-MM-dd')"; sqlcount += " and upday<=to_date('" + time.ToShortDateString() + "','yyyy-MM-dd')"; } if (!String.IsNullOrEmpty(combo_file_type.Text.Trim())) { sqlrows += " and filetype='" + combo_file_type.Text.Trim() + "'"; sqlcount += " and filetype='" + combo_file_type.Text.Trim() + "'"; } if (!String.IsNullOrEmpty(combo_writer.Text.Trim())) { sqlrows += " and writer='" + combo_writer.Text.Trim() + "'"; sqlcount += " and writer='" + combo_writer.Text.Trim() + "'"; } sqlrows += " and rownum<=" + (pagesize * pageIndex) + ") table_alias where table_alias.rowno>=" + ((pageIndex - 1) * pagesize + 1); int rows = int.Parse(OracleUtils.ExecuteScalar(OracleUtils.ConnectionString, CommandType.Text, sqlcount).ToString()); int pages = 0; if (rows % pagesize == 0) { pages = rows / pagesize; } else { pages = rows / pagesize + 1; } lastpage = pages; combo_page_num.Items.Clear(); for (int i = 1; i <= pages; i++) { combo_page_num.Items.Add(new ComboBoxItem(i.ToString(), i.ToString())); } if (lastpage == 1) { btn_page_first.Enabled = false; btn_page_pre.Enabled = false; btn_page_last.Enabled = false; btn_page_next.Enabled = false; } else if (currentpage == 1) { btn_page_first.Enabled = false; btn_page_pre.Enabled = false; btn_page_last.Enabled = true; btn_page_next.Enabled = true; } else if (currentpage == lastpage) { btn_page_first.Enabled = true; btn_page_pre.Enabled = true; btn_page_last.Enabled = false; btn_page_next.Enabled = false; } else { btn_page_first.Enabled = true; btn_page_pre.Enabled = true; btn_page_last.Enabled = true; btn_page_next.Enabled = true; } DataTable table = OracleUtils.ExecuteDataset(OracleUtils.ConnectionString, CommandType.Text, sqlrows).Tables[0]; dataGridViewX1.DataSource = table; } public void DownloadFile(string URL, string filename, ProgressBar prog) { float percent = 0; try { System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(URL); System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse(); long totalBytes = myrp.ContentLength; if (prog != null) { prog.Maximum = (int)totalBytes; } System.IO.Stream st = myrp.GetResponseStream(); System.IO.Stream so = new System.IO.FileStream(filename, System.IO.FileMode.Create); long totalDownloadedByte = 0; byte[] by = new byte[1024]; int osize = st.Read(by, 0, (int)by.Length); while (osize > 0) { totalDownloadedByte = osize + totalDownloadedByte; System.Windows.Forms.Application.DoEvents(); so.Write(by, 0, osize); if (prog != null) { prog.Value = (int)totalDownloadedByte; } osize = st.Read(by, 0, (int)by.Length); percent = (float)totalDownloadedByte / (float)totalBytes * 100; System.Windows.Forms.Application.DoEvents(); //必须加注这句代码,否则label1将因为循环执行太快而来不及显示信息 } so.Close(); st.Close(); } catch (System.Exception) { throw; } } private void FormDocumentManager_Load(object sender, EventArgs e) { IS_OPEN = true; dateTime_end.Value = DateTime.Now; dateTime_begin.Value = DateTime.Now.AddDays(-6); reloadGrid(currentpage); string sql = "select username from casic_userinfotest"; using (OracleDataReader reader = OracleUtils.ExecuteReader(OracleUtils.ConnectionString, CommandType.Text, sql)) { combo_writer.Items.Add(""); while (reader.Read()) { combo_writer.Items.Add(reader[0]); } } } private void FormDocumentManager_FormClosing(object sender, FormClosingEventArgs e) { IS_OPEN = false; } private void btn_add_Click(object sender, EventArgs e) { FrmDocumentEdit frm = new FrmDocumentEdit(); frm.reloadGrid += new ReloadUserDocumentGrid(reloadGrid); frm.ShowDialog(); } private void btn_query_Click(object sender, EventArgs e) { currentpage = 1; reloadGrid(currentpage); } private void dataGridViewX1_CellClick(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex < 0 || e.ColumnIndex < 0) { return; } string cell = dataGridViewX1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString(); try { if ("删除".Equals(cell)) { if (MessageBox.Show("确认删除吗?", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { string dbid = dataGridViewX1.Rows[e.RowIndex].Cells["dbid"].Value.ToString(); string sql = "delete from casic_userdocument where dbid=" + dbid; OracleUtils.ExecuteNonQuery(OracleUtils.ConnectionString, CommandType.Text, sql); reloadGrid(currentpage); MessageBox.Show("删除成功!", "结果", MessageBoxButtons.OK, MessageBoxIcon.Information); } } if ("预览".Equals(cell)) { string filename = dataGridViewX1.Rows[e.RowIndex].Cells["filename"].Value.ToString(); string url = ConfigurationManager.AppSettings["downurl"] + "/" + filename; string file = System.Environment.CurrentDirectory + "\\download\\" + filename; DownloadFile(url, file, progressBar1); progressBar1.Value = 0; switch (Path.GetExtension(filename)) { case ".doc": case ".docx": wordDocumentControl.Visible = true; excelDocumentControl.Visible = false; pdfDocumentControl.Visible = false; pictureDocumentControl.Visible = false; wordDocumentControl.LoadDocument(System.Environment.CurrentDirectory + "\\download\\" + filename); break; case ".xls": case ".xlsx": excelDocumentControl.Visible = true; wordDocumentControl.Visible = false; pdfDocumentControl.Visible = false; pictureDocumentControl.Visible = false; excelDocumentControl.LoadDocument(System.Environment.CurrentDirectory + "\\download\\" + filename); break; case ".pdf": pdfDocumentControl.Visible = true; wordDocumentControl.Visible = false; excelDocumentControl.Visible = false; pictureDocumentControl.Visible = false; pdfDocumentControl.LoadDocument(System.Environment.CurrentDirectory + "\\download\\" + filename); break; case ".jpg": case ".png": case ".gif": case ".bmp": pdfDocumentControl.Visible = false; wordDocumentControl.Visible = false; excelDocumentControl.Visible = false; pictureDocumentControl.Visible = true; pictureDocumentControl.ImageLocation = System.Environment.CurrentDirectory + "\\download\\" + filename; break; default: MessageBox.Show("不支持该格式文件预览", "结果", MessageBoxButtons.OK, MessageBoxIcon.Information); break; } } if ("下载".Equals(cell)) { string filename = dataGridViewX1.Rows[e.RowIndex].Cells["filename"].Value.ToString(); string ext = Path.GetExtension(filename); SaveFileDialog dlg = new SaveFileDialog(); dlg.Filter = "保存文档|*" + ext + ";"; dlg.FilterIndex = 2; dlg.RestoreDirectory = true; if (dlg.ShowDialog() == DialogResult.OK) { string url = ConfigurationManager.AppSettings["downurl"] + "/" + filename; string file = dlg.FileName; DownloadFile(url, file, progressBar1); } } } catch (Exception ex) { MessageBox.Show(cell + "操作失败:" + ex.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } }