我正在尝试通过mysql表数据在Excel工作表中插入数据。
'Feedstock', 'None', 'Naphtha', '5.00000', '2005', 'Y'
'Feedstock', 'None', 'Naphtha', '0.00000', '2006', 'Y'
'Feedstock', 'None', 'Naphtha', '0.00000', '2007', 'Y'
'Feedstock', 'None', 'Naphtha', '5.00000', '2008', 'Y'
'Feedstock', 'None', 'Naphtha', '5.00000', '2012', 'Y'
'Feedstock', 'None', 'Naphtha', '5.00000', '2014', 'Y'
'Feedstock', 'None', 'Naphtha', '5.00000', '2015', 'Y'
'Feedstock', 'None', 'Naphtha', '5.00000', '2016', 'Y'
'Feedstock', 'None', 'Naphtha', '5.00000', '2017', 'Y'
'Building Blocks', 'Olefins', 'Ethylene', '5.00000', '2005', 'Y'
'Building Blocks', 'Olefins', 'Ethylene', '5.00000', '2006', 'Y'
喜欢此表,并希望将此数据填写到Excel工作表中。
excel格式为:
2005-Y 2006-Y 2007-Y 2008-Y 2009-Y 2010-Y 2011-Y 2012-Y 2013-Y 2014-Y 2015-Y 2016-Y 2017-Y 2018-Y 2019-Y
Feedstock Naphtha 5 0 0 5 - - - 5 - 5 5 5 5 - -
Building Blocks (a)Olefine Ethylene 5 5 5 5 - - - 5 - 2.5 2.5 2.5 2.5 - -
Propylene 5 5 5 5 - - - 5 - 2.5 2.5 2.5 2.5 - -
Butadiene 5 5 5 5 - - - 5 - 2.5 2.5 2.5 2.5 - -
我尝试过:
import pymysql
from xlsxwriter.workbook import Workbook
from openpyxl import load_workbook
import openpyxl
from openpyxl.styles import Alignment, Font
sqlconn = ".............."
cursor = sqlconn.cursor()
wb = load_workbook('cpm_auto.xlsx')
sheet = wb.get_sheet_by_name('Sheet1')
max_col = (sheet.max_column)
max_row = (sheet.max_row)
for row in range(3,max_row):
for col in range(4,max_col):
periods = sheet.cell(row = 1,column = col).value
period = periods.replace('-Y','')
val = sheet.cell(row = row,column = 3).value
if val == 'Naphtha':
query = "select Value from CPMAI where Product = '%s' and Year = '%s'" %(val,period)
result = cursor.execute(query)
if result > 0:
values = cursor.fetchone()
if len(values) > 0:
prev_value = values[0]
print(prev_value)
sheet.cell(row = row,column = col).value = values[0]
else:
sheet.cell(row=row, column=col).value = prev_value
break
wb.save('cpm_auto.xlsx')
我的代码: 我尝试了此代码,但是此代码插入了具有值的年份的值,但是我想插入2005-2019年的所有年份值。请参阅excel格式,没有2009、2010、2011、2013、2018、2019的值,因此想要将前一年的值结转到该年。
答案 0 :(得分:0)
我不知道您数据库中的数据是什么。如果您遵循以下示例,则可以
public function endScreen():void
{
//removeEventListener(Event.EXIT_FRAME, endLoad);
ENDScore.text = "Score: " + String(scoreBar);
ENDScore.width = 200;
var endFormating:TextFormat = new TextFormat();
endFormating.size = 25;
endFormating.font = "Consolas";
endFormating.color= 0xFFFFFF;
ENDScore.x = stage.stageWidth/2 - ENDScore.width/2 + 10;
ENDScore.y = 415;
ENDScore.setTextFormat(endFormating);
endBtn.x = 660;
endBtn.y = 460;
endBtn.scaleX = 0.9;
endBtn.scaleY = 0.9;
addChild(endScr);
addChild(ENDScore);
addChild(endBtn);
closeDetect();
//trace("button");
//trace(scoreBar);
}
public function closeDetect(e:Event = null):void {
trace("button");
endBtn.addEventListener(MouseEvent.CLICK, closeGame);
}
public function closeGame(e:Event):void
{
//trace("button found");
//
//trace("button clicked");
////endBtn.visible = false;
//removeChild(enemyFighter);
//removeChild(enemyFighter2);
//removeChild(enemyFighter3);
//removeChild(enemyFighter4);
//removeChild(enemyFighter5);
//removeChild(lMass1);
//removeChild(lMass2);
//removeChild(player);
////removeChild(bullet);
//removeChild(endScr);
//removeChild(endBtn);
//removeChild(ENDScore);
//
//playerDeath = false;
//thePlayerDies = 3;
//scoreBar = 0;
//removeEventListener(MouseEvent.CLICK, closeGame);
////init();
//if (stage) init();
//else addEventListener(Event.ADDED_TO_STAGE, init);
fscommand("quit");
}
答案 1 :(得分:0)
我会在创建Excel工作表之前进行 计算。例如(如果表不是太大):
import pandas as pd
import numpy as np
# Load table into a DataFrame, see https://stackoverflow.com/questions/12047193
query = "select * from CPMAI"
result = cursor.execute(query)
df = pd.DataFrame(result.fetchall())
df.columns = result.keys()
pivot = df.pivot_table(values=["value"], index=["product"], columns=["year"], aggfunc=np.sum)
稍后,您可以遍历枢轴元素并填充Excel工作表