日期:2014-05-16  浏览次数:21077 次

数据导出excel
数据库使用mysql,请问怎样将一张表的表数据导出到excel中
我试过select * from tablename into outfile 'd:/test.xls'不行,有没有可实现的方法?
如果用java来实现,代码应该怎么写?

------解决方案--------------------
select * from tablename into outfile "d:\\test.xls"

------解决方案--------------------
你说的是不是apache的HSSF。
package test;

import java.io.FileOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Name;
import org.apache.poi.ss.usermodel.PrintSetup;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import test.bean.T7203Bean;
import test.dao.PoiDao;

public class TestFromDatabase {
private static final String title = "The Main Datas From T7203 Shows Below!";
private static final String[] head = { "SEQNUM", "EXCH_DATETIME",
"TRADE_PRICE", "ACVOL", "VWAP", "TOTAL" };

public static void main(String[] args) throws IOException, ParseException {
PoiDao dao = new PoiDao();
ArrayList<T7203Bean> list = dao.query();
Workbook wb = new HSSFWorkbook();
Map<String, CellStyle> styles = createStyles(wb);
Sheet sheet = wb.createSheet("T7203");
sheet.createRow((short) 0);

PrintSetup printSetup = sheet.getPrintSetup();
printSetup.setLandscape(true);
sheet.setFitToPage(true);
sheet.setHorizontallyCenter(true);

// Title
Row titleRow = sheet.createRow(0);
titleRow.setHeightInPoints(45);
Cell titleCell = titleRow.createCell(0);
titleCell.setCellValue(title);
titleCell.setCellStyle(styles.get("title"));
sheet.addMergedRegion(CellRangeAddress.valueOf("$A$1:$F$1"));

// Head
Row headerRow = sheet.createRow(1);
headerRow.setHeightInPoints(40);
Cell headerCell;
for (int i = 0; i < head.length; i++) {
headerCell = headerRow.createCell(i);
headerCell.setCellValue(head[i]);
headerCell.setCellStyle(styles.get("header"));
}
// Lines
Iterator<T7203Bean> it = list.iterator();
int rownum = 2;
while (it.hasNext()) {
Row row = sheet.createRow(rownum++);
T7203Bean bean = it.next();
Cell cell[] = new Cell[6];
for (int j = 0; j < head.length; j++) {
cell[j] = row.createCell(j);
if (j == 5) {
// Multiply(C3:I3)
String ref = "C" + rownum + ",D" + rownum;
cell[j].setCellFormula("PRODUCT(" + ref + ")");
cell[j].setCellStyle(styles.get("formula"));
} else if (j == 1) {
cell[j].setCellStyle(styles.get("date"));
} else {
cell[j].setCellStyle(styles.get("cell"));
}
}
cell[0].setCellValue(bean.getSeqnum());
// SimpleDateFormat sdf = new SimpleDateFormat("MM-dd hh:mm");
// cell[1].setCellValue(sdf.parse(bean.getExch_datetime()));
cell[1].setCellValue(bean.getExch_datetime());
cell[2].setCellValue(bean.getTrade_price());
cell[3].setCellValue(bean.getAcvol());
cell