zoukankan      html  css  js  c++  java
  • 很简单的导入导出excel

    1、导入excel数据到数据库

    很多时候我们要导入excel文件的数据到数据库,可以网上搜各种数据导出的方法(NPOI,Office.Interop.Excel.dll等),但是那些都需要在程序中开发,下面介绍很简单的导入方法,直接用excel中的公式生成更新的sql。例如有如下的城市 excel数据需要插入到数据库

    直接在第一条后面加公式 ="INSERT INTO T_Country (CountryName,CountryCode) VALUES('"&A2&"', '"&B2&"')"

    然后选中这一行按住ctrl,往下拉,sql就全部生成了,然后粘贴到数据库直接执行

    更新数据 ="update T_Country set CountryName='"&A2&"' where CountryCode='"&B2&"'"

    二、导出excel

    其实,excel文件也是一个xml文件,(把excel文件另存为xml格式),我们可以直接生成这样的xml数据然后转成xls,这就是我们的导出内容了

    1、基本内容

    xml的格式大致如下

    <?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.样式

    上面的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"?>
    ");
                stringBuilder.Append("<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
    ");
                stringBuilder.Append("xmlns:x="urn:schemas-microsoft-com:office:excel"
    ");
                stringBuilder.Append("xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
    ");
                stringBuilder.Append("xmlns:html="http://www.w3.org/TR/REC-html40">
    ");
                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="产品数据信息">
    ");
    
                stringBuilder.Append("<Table ss:StyleID="Default" ss:DefaultColumnWidth="200" ss:DefaultRowHeight="20">
    ");
                stringBuilder.Append("<Column ss:Index="1" ss:AutoFitWidth="0" ss:Width="30"/>
    ");
                stringBuilder.Append("<Column ss:Index="2" ss:AutoFitWidth="0" ss:Width="80"/>
    ");
                stringBuilder.Append("<Row ss:StyleID="s65" ss:AutoFitHeight="0" ss:Height="20">
    ");
                stringBuilder.Append("<Cell><Data ss:Type="String">序号</Data></Cell>
    ");
                stringBuilder.Append("<Cell><Data ss:Type="String">产品名称</Data></Cell>
    ");
                stringBuilder.Append("<Cell><Data ss:Type="String">产品简述</Data></Cell>
    ");
                stringBuilder.Append("<Cell><Data ss:Type="String">产品描述</Data></Cell>
    ");
                stringBuilder.Append("</Row>
    ");
    
                for (int i = 1; i <= 10; i++)
                {
                    stringBuilder.Append("<Row>
    ");
                    stringBuilder.Append("<Cell><Data ss:Type="String">" + i + "</Data></Cell>
    ");
                    stringBuilder.Append("<Cell><Data ss:Type="String">产品名称" + i + "</Data></Cell>
    ");
                    stringBuilder.Append("<Cell><Data ss:Type="String">产品简述" + i + "</Data></Cell>
    ");
                    stringBuilder.Append("<Cell><Data ss:Type="String">产品描述" + i + "</Data></Cell>
    ");
                    stringBuilder.Append("</Row>
    ");
                }
    
                stringBuilder.Append("</Table>
    ");
                stringBuilder.Append("</Worksheet>
    ");
                stringBuilder.Append("</Workbook>
    ");
                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数据 后台代码
  • 相关阅读:
    spring jdbctemplate 集成duckdb docker 镜像
    GLIBCXX_3.4.x 问题解决
    spring jdbctemplate 集成duckdb
    spring-boot-starter-data-jdbc Cannot determine a dialect Please provide a Dialect
    kotlin 学习 三 maven 集成+java 互调用
    kotlin 学习 二 命令行模式运行
    kotlin 学习 一 环境搭建
    pt-ioprofile io 瓶颈分析工具
    zombodb 3000 alpha1发布
    openfeign+retronfit http 访问
  • 原文地址:https://www.cnblogs.com/notevar/p/5983603.html
Copyright © 2011-2022 走看看