vs2010导入word表格

来源:互联网 发布:云盘搭建php源码 编辑:程序博客网 时间:2024/05/17 04:12
this.Import = function () {
        var fileName = sle_1.GetText();
        if (fileName == "") {
            requestor.MessageBox("提示", "请选择要导入的Word文件!", ICON.StopSign);
            sle_1.SetFocus();
            return;
        }
        if (!requestor.FileExists(fileName)) {
            requestor.MessageBox("提示", "文件 " + fileName + " 不存在,请检查!", ICON.StopSign);
            return;
        }
        if (requestor.GetFileSize(fileName) > (1024 * 1024)) {
            requestor.MessageBox("提示", "上传的文件不能大于1M,请重新选择!", ICON.StopSign);
            return;
        }
        //取文件内容的Base64的内容,即把文件由二进制,转为字符串,以便作为一般的表单内容上传到服务器
        var fileBase64 = requestor.GetFileBase64(fileName);
        if (fileBase64 == "")
            return;
       
        webReq.Invoke("AppointImport", "filedata=" + fileBase64);
        if (webReq.StatusCode() == 200) {
            requestor.MessageBox("提示", "导入成功!");   
        }
        else {
            requestor.MessageBox("提示", webReq.ResponseText(), ICON.StopSign);
        }
    }


protected void AppointImport()
        {
            MemoryStream ms = null;

            string years = Request["years"].ToString();
            byte[] fileData = GZip.UnCompress(Base64.Decode(Request.Form["filedata"].ToString()));
            ms = new MemoryStream(fileData, 0, fileData.Length);
            var document = ParseDocument1(ms);

            this.DBHelp.BeginTransAction();
            try
            {
                 SqlCommand cmd = this.DBHelp.GetCommand("INSERT INTO T_Appoint"+
                "(ID,Years,Name,Sex) Values(" +
                "@ID,@Years,@Name,@Sex)");
                cmd.Parameters.Add(new SqlParameter("@ID", document.ID));
                cmd.Parameters.Add(new SqlParameter("@Years", years));
                cmd.Parameters.Add(new SqlParameter("@Name", document.Name));
                cmd.Parameters.Add(new SqlParameter("@Sex", document.Sex));

                cmd.ExecuteNonQuery();

                this.DBHelp.Commit();
            }
            catch (Exception ex)
            {
                this.DBHelp.Rollback();
                this.SetErrorInfo("导入时发生错误。\r\n错误信息为:\r\n" + ex.Message);

            }
        }


public static AppointAprovalSubmitModel ParseDocument1(Stream stream)
        {
            try
            {
                var doc = DocX.Load(stream);
                var tables = doc.Tables;
                if (tables.Count == 0)
                    throw new Exception("The uploaded document does not contain a table");
                var table = tables[0];

                return new AppointAprovalSubmitModel()
                {
                    ID = System.Guid.NewGuid().ToString("D"),
                   
                    Name = table.GetCell(0, 1),
                    Sex = table.GetCell(0,3),
                    Nation = table.GetCell(0, 5),
                                        
                    //DateUploaded = DateTime.Now,
                };
            }
            catch (Exception ex)
            {
                throw new Exception("Unable to parse the uploaded document. See inner exception for details", ex);
            }
        }

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using TXSoft.DataStore;
using System.Text;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Novacode;
using TXSoft.Common;
using System.Reflection;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;


0 0