数据库使用mysql,请问怎样将一张表的表数据导出到excel中
我试过select * from tablename into outfile 'd:/test.xls'不行,有没有可实现的方法?
如果用java来实现,代码应该怎么写?
我试过select * from tablename into outfile 'd:/test.xls'不行,有没有可实现的方法?
如果用java来实现,代码应该怎么写?
解决方案 »
- sql语句
- 安装MYSQL失败
- Mysql 存储过程中游标的使用问题
- 急!!! mysql导入sql文件问题
- 有没有mysql的分页存储过程!!
- 类似mysql数据库ID自动增加
- 无意中发现的也许是MYSQL的一个BUG!!!
- mysql error 1215: Cannot add foreign key constraint
- php+mysql如何快速输出结果
- sql 数据搜索
- 编写存储过程时出现:Recursive stored functions and triggers are not allowed.
- show innodb status有关Spin Waits,Spin Rounds,Os Waits是如何解释的
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[4].setCellValue((double) bean.getVwap());
}
// Set Names!
// createNames(wb);
// finally set column widths, the width is measured in units of 1/256th
// of a character width
sheet.setColumnWidth(0, 15 * 256);
for (int i = 1; i < 5; i++) {
sheet.setColumnWidth(i, 20 * 256); // 6 characters wide
}
sheet.setColumnWidth(5, 25 * 256); // 10 characters wide // Write the output to a file
String file = "src/results/queryFromT7203.xls";
if (wb instanceof XSSFWorkbook)
file += "x";
FileOutputStream out = new FileOutputStream(file);
wb.write(out);
out.close(); } private static Map<String, CellStyle> createStyles(Workbook wb) {
Map<String, CellStyle> styles = new HashMap<String, CellStyle>();
CellStyle style;
Font titleFont = wb.createFont();
titleFont.setFontHeightInPoints((short) 18);
titleFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
style = wb.createCellStyle();
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
style.setFont(titleFont);
styles.put("title", style); Font monthFont = wb.createFont();
monthFont.setFontHeightInPoints((short) 11);
monthFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
monthFont.setColor(IndexedColors.WHITE.getIndex());
style = wb.createCellStyle();
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
style.setFont(monthFont);
style.setBorderRight((short) 5);
style.setBorderLeft((short) 5);
style.setBorderTop((short) 5);
style.setBottomBorderColor(IndexedColors.BLUE.getIndex());
style.setTopBorderColor(IndexedColors.BLACK.getIndex());
style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
style.setRightBorderColor(IndexedColors.BLACK.getIndex());
style.setWrapText(true);
styles.put("header", style); style = wb.createCellStyle();
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setWrapText(true);
style.setBorderRight(CellStyle.BORDER_THIN);
style.setRightBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderLeft(CellStyle.BORDER_THIN);
style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderTop(CellStyle.BORDER_THIN);
style.setTopBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderBottom(CellStyle.BORDER_THIN);
style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
styles.put("cell", style); style = wb.createCellStyle();
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
style.setDataFormat(wb.createDataFormat().getFormat("#,#000.00"));
styles.put("formula", style); style = wb.createCellStyle();
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setWrapText(true);
style.setBorderRight(CellStyle.BORDER_THIN);
style.setRightBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderLeft(CellStyle.BORDER_THIN);
style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderTop(CellStyle.BORDER_THIN);
style.setTopBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderBottom(CellStyle.BORDER_THIN);
style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
style.setDataFormat(wb.createDataFormat().getFormat("m/d/yy"));
styles.put("date", style); return styles;
} public static Name createNames(Workbook wb) {
Name name;
name = wb.createName();
createNames(wb).setNameName("trade_price*acvol");
createNames(wb).setRefersToFormula("'T7203'!$E$2");
return name;
}
}
可能我说的不太清楚,我的意思是:从服务器中提取数据库的数据,以excel的形式保存在本地,关键是要弹出保存对话框,也可以说成是以下载的形式完成,我用的是struts架构
import java.io.IOException;public class ProdDataTest {
public static void main(String[] args) {
try {
FileWriter fw = new FileWriter("C:\\csv_test.csv");
fw.write("aaa,bbb,ccc,ddd,eee,fff,ggg,hhh\r\n");
fw.write("aa1,bb1,cc1,dd1,ee1,ff1,gg1,hh1\r\n");
fw.write("aa2,bb2,cc2,dd2,ee2,ff2,gg2,hh2\r\n");
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}}