excel表格SQL
1. 如果excel中想使用sql語句怎麼使用
在execl中無法使用sql語句!因為excel沒有內嵌sql語言,如果非想使用,那麼你應該選用access而不是excel。但是在sql
server等資料庫中可以使用excel,也就是將excel轉成資料庫。
否則,只能使用excel中自帶的各種函數實現。比如你要統計一個數據范圍,可以考慮使用if,如果計數的話,可以考慮使用ifcount,等等還有類似的平均值等統計函數均可以在excel中查看!
如果是對輸入值有限制的話,可以考慮使用vba進行宏編輯!
2. 如何把Excel數據轉化成SQL語句
1、例如我想把圖1的數據給添加到圖2的資料庫中;
3. excel怎麼連接sql資料庫
打開EXCEL,新建一個空白工作表,點擊【數據】菜單頁面中的【自其他來源】,在下拉菜單中選項【來自SQL Server】選項。
4. Excel中如何用SQL進行多工作簿匯總
對於經常使用Excel的人來說,在Excel中使用SQL能高效處理你的數據,但據我所知Excel中的SQL語法與MySQL數據的語法還有點不一樣。現在我就來實例說明如何用SQL語句對多個工作簿進行匯總。假如我們要匯總如下三個工作簿
1. 學生信息表
2. 學生成績表
3. 學生興趣表
將這三個工作簿按相同學號進行合並,大致分如下幾步:
1. 載入數據
在Excel中,依次點擊:數據-自其它來源-來自Microsoft Query-Excel Files*
然後再找到你Excel表格的存放目錄,如下圖
點擊下面你要載入的Excel數據表,進入下面頁面
將上圖可用的三張表都加入到右邊查詢結果中的列,加入完成後點擊下一步,數據就載入完成了。
2. 編寫SQL代碼並執行
接下來點擊上面的SQL按鈕開始寫SQL合並代碼。單純的合並代碼如下
SELECT
tm.*,
tn.興趣愛好 AS 興趣愛好
FROM(
SELECT
ta.學號 AS 學號,
ta.姓名 AS 姓名,
ta.年齡 AS 年齡,
ta.性別 AS 性別,
ta.身高 AS 身高,
ta.出生地 AS 出生地,
tb.語文成績 AS 語文成績,
tb.數學成績 AS 數學成績
FROM(
SELECT
tt.學號 AS 學號,
tt.姓名 AS 姓名,
tt.年齡 AS 年齡,
tt.性別 AS 性別,
tt.身高 AS 身高,
tt.出生地 AS 出生地
FROM [學生基本信息表$] tt
)ta
INNER JOIN(
SELECT
tt.學號 AS 學號,
tt.語文成績 AS 語文成績,
tt.數學成績 AS 數學成績
FROM [學生成績表$] tt
)tb
ON ta.學號=tb.學號
)tm
INNER JOIN(
SELECT
tt.學號 AS 學號,
tt.興趣愛好 AS 興趣愛好
FROM [學生興趣表$] tt
)tn
ON tm.學號=tn.學號
將代碼寫好後復制放入文本框中,點擊確定按鈕,就開始執行SQL語句。結果如下
這樣就完成了三個工作簿的數據合並,接下來將數據返回Excel表。
3. 返回數據到Excel表
點擊左上角的文件-將數據返回Microsoft Excel,彈出如下頁面
選擇數據的存放位置為新工作表,然後點擊確定即可,最終得到如下數據表
這樣就完成了三個工作簿的數據合並。
Tips:
1. 由於Excel中執行SQL語句時沒有比較詳細的報錯信息,如果SQL代碼語法有錯誤有可能執行會報錯,這個問題可以上網查詢或者直接在評論區問我。
2. 在Excel SQL中常用到的合並操作有INNER JOIN、LEFT JOIN、RIGHT JOIN、UNION、UNION ALL等,但不支持FULL JOIN。更復雜的需求都有一定的處理技巧。
5. 如何在excel表格中讀取sql資料庫數據
excel程序自帶的外部數據連接功能,可以連接sql伺服器。具體步驟為:
1、選擇【數據】選項卡,在【獲取外部數據】分組,找到【自其他來源】。
2、單擊【自其他來源】按鈕,打開【選擇來源】下拉菜單。
3、選擇【來自sql
server】打開【數據連接向導】對話框。
4、填寫伺服器名稱、登錄的用戶名及密碼等信息,即可成功連接。
6. 如何在Excel中使用SQL語言實現數據查詢
1、選中表格後單擊右鍵選擇「定義名稱」。
2、單擊後,出現命名對話框。
3、這里將表1和表2分別命名為Table1和Table2。
4、選擇上方的「數據」選項卡,選擇「自其他來源」下的「來自Microsoft Query 」選項。
5、在彈出的對話框中選擇Excel Files*那一項,並且把對話框下面的「使用「查詢向導」創建/編輯查詢」勾掉。
6、點擊「確定」,便出現「選擇工作簿」的對話框,這里選擇包含表1和表2的工作表xxx.xlsx。
7、點擊確定後之後彈出添加表的對話框。
8、點擊確定後之後彈出添加表的對話框。
9、輸入SQL語句的按鈕,彈出輸入SQL語句的對話框 輸入SQL,看到篩選出來的數據表。
10、將篩選出來的數據表再返回至Excel工作表當中,選擇菜單中的「文件」——「將數據返回Microsoft Excel」。
7. 怎樣把Excel表格導入到SQL資料庫中
下面是使用java實現的,將Excel數據表中的數據導入到資料庫里裡面。
public class ReadExcel {
/**
* 對外提供讀取excel 的方法
* */
public static List<List<Object>> readExcel(File file) throws IOException {
String fileName = file.getName();
String extension = fileName.lastIndexOf(".") == -1 ? "" : fileName
.substring(fileName.lastIndexOf(".") + 1);
if ("xls".equals(extension)) {
return read2003Excel(file);
} else if ("xlsx".equals(extension)) {
return read2007Excel(file);
} else {
throw new IOException("不支持的文件類型");
}
}
/**
* 讀取 office 2003 excel
*
* @throws IOException
* @throws FileNotFoundException
*/
private static List<List<Object>> read2003Excel(File file)
throws IOException {
List<List<Object>> list = new LinkedList<List<Object>>();
HSSFWorkbook hwb = new HSSFWorkbook(new FileInputStream(file));
HSSFSheet sheet = hwb.getSheetAt(0);
Object value = null;
HSSFRow row = null;
HSSFCell cell = null;
int counter = 0;
for (int i = sheet.getFirstRowNum(); counter < sheet
.getPhysicalNumberOfRows(); i++) {
row = sheet.getRow(i);
if (row == null) {
continue;
} else {
counter++;
}
List<Object> linked = new LinkedList<Object>();
for (int j = row.getFirstCellNum(); j <= row.getLastCellNum(); j++) {
cell = row.getCell(j);
if (cell == null) {
continue;
}
DecimalFormat df = new DecimalFormat("0");// 格式化 number String
// 字元
SimpleDateFormat sdf = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");// 格式化日期字元串
DecimalFormat nf = new DecimalFormat("0.00");// 格式化數字
switch (cell.getCellType()) {
case XSSFCell.CELL_TYPE_STRING:
// System.out.println(i + "行" + j + " 列 is String type");
value = cell.getStringCellValue();
break;
case XSSFCell.CELL_TYPE_NUMERIC:
/*System.out.println(i + "行" + j
+ " 列 is Number type ; DateFormt:"
+ cell.getCellStyle().getDataFormatString());*/
if ("@".equals(cell.getCellStyle().getDataFormatString())) {
value = df.format(cell.getNumericCellValue());
} else if ("General".equals(cell.getCellStyle()
.getDataFormatString())) {
value = nf.format(cell.getNumericCellValue());
} else {
value = sdf.format(HSSFDateUtil.getJavaDate(cell
.getNumericCellValue()));
}
break;
case XSSFCell.CELL_TYPE_BOOLEAN:
// System.out.println(i + "行" + j + " 列 is Boolean type");
value = cell.getBooleanCellValue();
break;
case XSSFCell.CELL_TYPE_BLANK:
// System.out.println(i + "行" + j + " 列 is Blank type");
value = "";
break;
default:
// System.out.println(i + "行" + j + " 列 is default type");
value = cell.toString();
}
if (value == null || "".equals(value)) {
continue;
}
linked.add(value);
}
list.add(linked);
}
return list;
}
/**
* 讀取Office 2007 excel
* */
private static List<List<Object>> read2007Excel(File file)
throws IOException {
List<List<Object>> list = new LinkedList<List<Object>>();
// 構造 XSSFWorkbook 對象,strPath 傳入文件路徑
XSSFWorkbook xwb = new XSSFWorkbook(new FileInputStream(file));
// 讀取第一章表格內容
XSSFSheet sheet = xwb.getSheetAt(0);
Object value = null;
XSSFRow row = null;
XSSFCell cell = null;
int counter = 0;
for (int i = sheet.getFirstRowNum(); counter < sheet
.getPhysicalNumberOfRows(); i++) {
row = sheet.getRow(i);
if (row == null) {
continue;
} else {
counter++;
}
List<Object> linked = new LinkedList<Object>();
for (int j = row.getFirstCellNum(); j <= row.getLastCellNum(); j++) {
cell = row.getCell(j);
if (cell == null) {
continue;
}
DecimalFormat df = new DecimalFormat("0");// 格式化 number String
// 字元
SimpleDateFormat sdf = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");// 格式化日期字元串
DecimalFormat nf = new DecimalFormat("0.00");// 格式化數字
switch (cell.getCellType()) {
case XSSFCell.CELL_TYPE_STRING:
System.out.println(i + "行" + j + " 列 is String type");
value = cell.getStringCellValue();
break;
case XSSFCell.CELL_TYPE_NUMERIC:
/* System.out.println(i + "行" + j
+ " 列 is Number type ; DateFormt:"
+ cell.getCellStyle().getDataFormatString());*/
if ("@".equals(cell.getCellStyle().getDataFormatString())) {
value = df.format(cell.getNumericCellValue());
} else if ("General".equals(cell.getCellStyle()
.getDataFormatString())) {
value = nf.format(cell.getNumericCellValue());
} else {
value = sdf.format(HSSFDateUtil.getJavaDate(cell
.getNumericCellValue()));
}
break;
case XSSFCell.CELL_TYPE_BOOLEAN:
// System.out.println(i + "行" + j + " 列 is Boolean type");
value = cell.getBooleanCellValue();
break;
case XSSFCell.CELL_TYPE_BLANK:
// System.out.println(i + "行" + j + " 列 is Blank type");
value = "";
break;
default:
// System.out.println(i + "行" + j + " 列 is default type");
value = cell.toString();
}
if (value == null || "".equals(value)) {
continue;
}
linked.add(value);
}
list.add(linked);
}
return list;
}
public static void main(String[] args) {
try {
readExcel(new File("D:\Java\apache-tomcat-8.0.26\webapps\poi\docs\testRead.xls"));
// readExcel(new File("D:\test.xls"));
/*
String docsPath = request.getSession(true).getServletContext()
.getRealPath("docs");
String fileName = "testRead.xls";
String filePath = docsPath;
if (EPlatform.Windows.equals(OSinfo.getOSname())) {
filePath = filePath + "\" + fileName;
} else {
filePath = filePath + "/" + fileName;
}
filePath = "E:\testRead.xls";
List<List<Object>> list = readExcel(new File(filePath));
request.setAttribute("list", list);
RequestDispatcher dispatcher = request
.getRequestDispatcher("/read.jsp");
dispatcher.forward(request, response);
*/
} catch (IOException e) {
e.printStackTrace();
}
}
}
8. 怎麼將excel表格導入到sql資料庫
在資料庫初始階段,我們有些數據在EXCEL中做好之後,需要將EXCEL對應列名(導入後對應資料庫表的欄位名),對應sheet(改名為導入資料庫之後的表名)導入指定資料庫,相當於導入一張表的整個數據。導入之前需要檢查是否存在同名的表,導入的數據表以「$」結尾就是為了避免表重復,以作區分。下面就來看看具體操作步驟。
http://jingyan..com/article/358570f66145a6ce4724fcba.html
9. 如何將excel表格導入sql
1、打開企業管理器,打開要導入數據的資料庫,在表上按右鍵,所有任務-->導入數據,彈出DTS導入/導出向導,按 下一步 ,
2、選擇數據源 Microsoft Excel 97-2000,文件名 選擇要導入的xls文件,按 下一步 ,
3、選擇目的 用於SQL Server 的Microsoft OLE DB提供程序,伺服器選擇本地(如果是本地資料庫的話,如 VVV),使用SQL Server身份驗證,用戶名sa,密碼為空,資料庫選擇要導入數據的資料庫(如 client),按 下一步 ,
4、選擇 用一條查詢指定要傳輸的數據,按 下一步 ,
5、按 查詢生成器,在源表列表中,有要導入的xls文件的列,將各列加入到右邊的 選中的列 列表中,這一步一定要注意,加入列的順序一定要與資料庫中欄位定義的順序相同,否則將會出錯,按 下一步 ,
6、選擇要對數據進行排列的順序,在這一步中選擇的列就是在查詢語句中 order by 後面所跟的列,按 下一步 ,
7、如果要全部導入,則選擇 全部行,按 下一步,
8、則會看到根據前面的操作生成的查詢語句,確認無誤後,按 下一步,
9、會看到 表/工作表/Excel命名區域 列表,在 目的 列,選擇要導入數據的那個表, 按 下一步,
10、選擇 立即運行,按 下一步,
11、會看到整個操作的摘要,按 完成 即可