很簡單的導入導出excel

一、導入excel數據到數據庫html

不少時候咱們要導入excel文件的數據到數據庫,能夠網上搜各類數據導出的方法(NPOI,Office.Interop.Excel.dll等),可是那些都須要在程序中開發,下面介紹很簡單的導入方法,直接用excel中的公式生成更新的sql。例若有以下的城市 excel數據須要插入到數據庫sql

直接在第一條後面加公式 ="INSERT INTO T_Country (CountryName,CountryCode) VALUES('"&A2&"', '"&B2&"')"數據庫

而後選中這一行按住ctrl,往下拉,sql就所有生成了,而後粘貼到數據庫直接執行app

更新數據 ="update T_Country set CountryName='"&A2&"' where CountryCode='"&B2&"'"ide

2、導出excel字體

其實,excel文件也是一個xml文件,(把excel文件另存爲xml格式),咱們能夠直接生成這樣的xml數據而後轉成xls,這就是咱們的導出內容了ui

一、基本內容spa

xml的格式大體以下excel

<?xml version="1.0" encoding="UTF-8"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<Styles>
    <Style ss:ID="Default" ss:Name="Normal">
    <Alignment ss:Horizontal="Center" ss:Vertical="Center"/>
    <Font ss:FontName="宋體" ss:Size="11" ss:Color="#000000"/>
    </Style>
</Styles>
<Worksheet ss:Name="產品數據信息">
<Table ss:StyleID="Default" ss:DefaultColumnWidth="200" ss:DefaultRowHeight="25">
<Column ss:Index="1" ss:AutoFitWidth="0" ss:Width="30"/>
<Column ss:Index="2" ss:AutoFitWidth="0" ss:Width="80"/>
<Row ss:AutoFitHeight="0" ss:Height="20">
<Cell><Data ss:Type="String">序號</Data></Cell>
<Cell><Data ss:Type="String">產品名稱</Data></Cell>
<Cell><Data ss:Type="String">產品簡述</Data></Cell>
<Cell><Data ss:Type="String">產品描述</Data></Cell>
</Row>
<Row>
<Cell><Data ss:Type="String">1</Data></Cell>
<Cell><Data ss:Type="String">產品名稱1</Data></Cell>
<Cell><Data ss:Type="String">產品簡述1</Data></Cell>
<Cell><Data ss:Type="String">產品描述1</Data></Cell>
</Row>
<Row>
<Cell><Data ss:Type="String">2</Data></Cell>
<Cell><Data ss:Type="String">產品名稱2</Data></Cell>
<Cell><Data ss:Type="String">產品簡述2</Data></Cell>
<Cell><Data ss:Type="String">產品描述2</Data></Cell>
</Row>
</Table>
</Worksheet>
</Workbook>

2.樣式code

上面的xml包含一些樣式Style 和工做表Worksheet和每一行Row,在這裏咱們能夠自定義一些樣式,好比第一行的標題居中,加粗等,代碼以下

<Style ss:ID="s65">
<Alignment ss:Horizontal="Center" ss:Vertical="Center"/>
<Font ss:FontName="宋體" ss:Size="11" ss:Color="#000000" ss:Bold="1"/>
</Style>

ss:Horizontal:水平居中,ss:Vertical:垂直居中,ss:FontName:字體名稱(宋體,微軟雅黑),ss:Size="11":字體大小,ss:Color:字體顏色(能夠寫顏色代碼或red green),ss:Bold:字體加粗(這裏只能寫0和1,0表示不加粗,1加粗)

3.工做表內容

<Worksheet ss:Name="產品數據信息">
<Table ss:StyleID="Default" ss:DefaultColumnWidth="200" ss:DefaultRowHeight="25">
<Column ss:Index="1" ss:AutoFitWidth="0" ss:Width="30"/>
<Column ss:Index="2" ss:AutoFitWidth="0" ss:Width="80"/>
<Row ss:AutoFitHeight="0" ss:Height="20">
<Cell><Data ss:Type="String">序號</Data></Cell>
<Cell><Data ss:Type="String">產品名稱</Data></Cell>
<Cell><Data ss:Type="String">產品簡述</Data></Cell>
<Cell><Data ss:Type="String">產品描述</Data></Cell>
</Row>
<Row>
<Cell><Data ss:Type="String">1</Data></Cell>
<Cell><Data ss:Type="String">產品名稱1</Data></Cell>
<Cell><Data ss:Type="String">產品簡述1</Data></Cell>
<Cell><Data ss:Type="String">產品描述1</Data></Cell>
</Row>
<Row>
<Cell><Data ss:Type="String">2</Data></Cell>
<Cell><Data ss:Type="String">產品名稱2</Data></Cell>
<Cell><Data ss:Type="String">產品簡述2</Data></Cell>
<Cell><Data ss:Type="String">產品描述2</Data></Cell>
</Row>
</Table>
</Worksheet>

ss:Name:對應的是excel右下的工做表名稱,ss:DefaultColumnWidth:默認的列寬,ss:DefaultRowHeight:默認行高,是單獨對某一列定義(例如寬度高度),ss:Index:對應的列的序號,從1開始,若是不定義每一列的列度,將會使用默認列寬和高度

導出excel數據 後臺代碼

 /// <summary>
        /// 導出excle
        /// </summary>
        public ActionResult ExportExcelData()
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
            stringBuilder.Append("<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"\n");
            stringBuilder.Append("xmlns:x=\"urn:schemas-microsoft-com:office:excel\"\n");
            stringBuilder.Append("xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"\n");
            stringBuilder.Append("xmlns:html=\"http://www.w3.org/TR/REC-html40\">\n");
            stringBuilder.Append("<Styles><Style ss:ID=\"Default\" ss:Name=\"Normal\"><Alignment ss:Horizontal=\"Center\" ss:Vertical=\"Center\"/><Font ss:FontName=\"宋體\" ss:Size=\"11\" ss:Color=\"#000000\"/></Style>");
            stringBuilder.Append("<Style ss:ID=\"s65\"><Alignment ss:Horizontal=\"Center\" ss:Vertical=\"Center\"/><Font ss:FontName=\"宋體\" x:CharSet=\"134\" ss:Size=\"11\" ss:Color=\"#000000\" ss:Bold=\"1\"/></Style></Styles>");
            stringBuilder.Append("<Worksheet ss:Name=\"產品數據信息\">\n");

            stringBuilder.Append("<Table ss:StyleID=\"Default\" ss:DefaultColumnWidth=\"200\" ss:DefaultRowHeight=\"20\">\n");
            stringBuilder.Append("<Column ss:Index=\"1\" ss:AutoFitWidth=\"0\" ss:Width=\"30\"/>\n");
            stringBuilder.Append("<Column ss:Index=\"2\" ss:AutoFitWidth=\"0\" ss:Width=\"80\"/>\n");
            stringBuilder.Append("<Row ss:StyleID=\"s65\" ss:AutoFitHeight=\"0\" ss:Height=\"20\">\n");
            stringBuilder.Append("<Cell><Data ss:Type=\"String\">序號</Data></Cell>\n");
            stringBuilder.Append("<Cell><Data ss:Type=\"String\">產品名稱</Data></Cell>\n");
            stringBuilder.Append("<Cell><Data ss:Type=\"String\">產品簡述</Data></Cell>\n");
            stringBuilder.Append("<Cell><Data ss:Type=\"String\">產品描述</Data></Cell>\n");
            stringBuilder.Append("</Row>\n");

            for (int i = 1; i <= 10; i++)
            {
                stringBuilder.Append("<Row>\n");
                stringBuilder.Append("<Cell><Data ss:Type=\"String\">" + i + "</Data></Cell>\n");
                stringBuilder.Append("<Cell><Data ss:Type=\"String\">產品名稱" + i + "</Data></Cell>\n");
                stringBuilder.Append("<Cell><Data ss:Type=\"String\">產品簡述" + i + "</Data></Cell>\n");
                stringBuilder.Append("<Cell><Data ss:Type=\"String\">產品描述" + i + "</Data></Cell>\n");
                stringBuilder.Append("</Row>\n");
            }

            stringBuilder.Append("</Table>\n");
            stringBuilder.Append("</Worksheet>\n");
            stringBuilder.Append("</Workbook>\n");
            Response.Clear();
            Response.AppendHeader("Content-Disposition", "attachment;filename=產品基本數據信息" + DateTime.Now.ToString("yyyyMMdd") + ".xls");
            Response.Charset = "gb2312"; Response.ContentType = "application/ms-excel";
            Response.Write(stringBuilder.ToString());
            Response.End();
            return null;
        }
導出excel數據 後臺代碼
相關文章
相關標籤/搜索